我试图让 if 语句来检测这两个数组是否有相等的部分。
const winningCombos = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
const mySpots = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
function test() {
winningCombos[0].splice(0, 3, 'testsss')
//position 0 remove 1
if (winningCombos[0] || winningCombos[1] || winningCombos[2] === mySpots[0] || mySpots[1] || mySpots[2]) {
document.getElementById("test").innerHTML = 'sucess'
} else {
document.getElementById("test").innerHTML = 'fail'
}
}
但我也试图让 if 语句变得像
if ((winningCombos[0] OR winningCombos[1] OR winningCombos[2]) is equal to(mySpots[0] || mySpots[1] || mySpots[2]) {
console.log('success');
};
大家能找到什么解决办法吗?
您必须迭代数组并比较项目
function findSimilarItems(a, b) {
let similarities = [];
if (a.length !== b.length) {
throw new Error("Arrays or their rows are not of the same length.");
}
for (let i = 0; i < a.length; i++) {
let rowSimilarities = [];
for (let j = 0; j < a[i].length; j++) {
if (a[i][j] === b[i][j]) {
rowSimilarities.push(a[i][j]);
}
}
similarities.push(rowSimilarities);
}
return similarities;
}
const a = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[1, 2, 3],
[5, 9, 0]
];
const b = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[1, 2],
[1, 2]
];
console.log(findSimilarItems(a, b));