简体   繁体   English

如果检查的值在嵌套数组中,如何获取数组的索引?

[英]how to get an index of an array if the checked value is in the nested array?

I have an array like this我有一个这样的数组

const myArray = [ [ false ], [ true ], [ false ] ] 

i want to get index of an element that has value == true from array above.我想从上面的数组中获取 value == true 的元素的索引。

so because the true from array above is in the second element, then I want to get 1 as the index result所以因为上面数组中的true在第二个元素中,所以我想得到1作为索引结果

how to do that?怎么做?

You just need add filter for this,你只需要为此添加过滤器,

const myArray = [ [ false ], [ true ], [ false ] ] 

let result = myArray.filter(function(value) {
    return value[0]=== true;
});
const arrays = [ [ false ], [ true ], [ false ] ] 

const index = [].concat.apply([], arrays).findIndex(value => value === true)
const idx = myArray.findIndex(i => i[0]);

you can loop on your array with map method您可以使用 map 方法在您的阵列上循环


const arr = [ [ false ], [ true ], [ false ] ] 

arr.map((item, index) => {

if (item[0]) {
  console.log(index)
}

})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM