偶然的你
您可以使用嵌套地图来做到这一点const array2d = [ [1, 2], [3, 4], [5, 6], [7, 8],]const array1 = [1, 8]const output = array2d.map(sub => sub.map(el => array1.includes(el) ? 0 : 1))console.log(output)
开心每一天1111
简单的嵌套forEachwithincludes就可以了:array2d = [ [1, 2], [3, 4], [5, 6], [7, 8],];array1 = [1, 8];array2d.forEach((arr, i) => { arr.forEach((el, j) => { array2d[i][j] = array1.includes(el) ? 0 : 1; });});//works as a on-liner too://array2d.forEach((arr, i) => arr.forEach((el, j) => array2d[i][j] = array1.includes(el) ? 0 : 1));console.log(array2d);此解决方案的一个优点是,如果您想保留对数组变量的引用(不创建新数组)