【发布时间】:2021-12-01 18:36:30
【问题描述】:
我可以找到一个数组是否存在于另一个数组中:
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 的高阶函数可以找到索引吗?我看不到类似的问题/答案,只是语法更简洁
谢谢
【问题讨论】:
标签: javascript arrays higher-order-functions