我有这个数组
var arr = [["present",["John","Josh","Jay"]],["absent",["May","Mary","Mary Jane"]]];
var arr2 = [["J",["John","Josh","Jay"]],["M",["May","Mary","Mary Jane"]]];
我想获取值
data[0]
的数组的"Mary"
。因此,在我的示例中,我将获得的值为
"absent"
。我还想获取值
"Josh"
的数组arr2的索引。因此,在第二个数组中,我将获得的值为0。
如果可以使用下划线js,我们可以使用它。
我尝试使用
_.contains()
,但失败了。这些数组也在淘汰赛js中使用。
最佳答案
完成这项工作的另一种方法可能如下:
var a1 = [["present",["John","Josh","Jay"]],["absent",["May","Mary","Mary Jane"]]],
a2 = [["J",["John","Josh","Jay"]],["M",["May","Mary","Mary Jane"]],["S",["Sally","Sam","Sammy Davis"]]],
getStatus = (a,n) => a.find(e => e[1].indexOf(n) !== -1)[0],
getIndex = (a,n) => a.findIndex(e => e[1].indexOf(n) !== -1);
console.log(getStatus(a1,"Mary"));
console.log(getIndex(a2,"Sammy Davis"));