我可以找出一个数组是否存在于另一个数组中:
const arr1 = [[1,2,3],[2,2,2],[3,2,1]];
const match = [2,2,2];
// Does match exist
const exists = arr1.some(item => {
return item.every((num, index) => {
return match[index] === num;
});
});
我可以找到该数组的索引:
let index;
// Index of match
for(let x = 0; x < arr1.length; x++) {
let result;
for(let y = 0; y < arr1[x].length; y++) {
if(arr1[x][y] === match[y]) {
result = true;
} else {
result = false;
break;
}
}
if(result === true) {
index = x;
break;
}
}
但是使用JS的高阶函数能否找到索引呢?我没有看到类似的问题/答案,语法方面更加简洁。
谢谢