我有一个如下所示的数组。
array = [[false, 1, "label", "label value", null],[false, 2, "label1", "label1", null]]
我想找到与 checkNum 匹配的行并返回该行。 checkNum 与第二个索引元素进行比较。 我不想放置像下面这样的 for 循环,
checkNum = 1;
for (let i = 0; i < array.length; i++) {
if ((array[i][1]) === checkNum) {
}
}
最佳答案
使用Array.filter()
获取符合条件的项目数组,或 Array.find()
获取第一个匹配的项目。
const array = [[false, 1, "label", "label value", null],[false, 2, "label1", "label1", null]]
const checkNum = 1
console.log(array.filter(({ 1: n }) => n === checkNum)) // array of items
console.log(array.find(({ 1: n }) => n === checkNum)) // 1st item found
关于javascript - 如何在javascript数组中查找值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55871364/