如何将数组与自身进行比较,而不是对同一对象进行比较
var i = 0;
var boo = true;
while (i < this.bombs.length) {
if (this.bombs[i] === this.bombs[i]) {
console.log("true");
boo = true;
} else {
boo = false;
}
i++;
return boo;
}
但不应使用bombs[i]
bombs[i]
答案 0 :(得分:1)
您需要两个嵌套循环,并在指向同一个对象时跳过。这是一个天真的实现
let bombs = [{x: 1, y:2}, {x: 1, y: 4}, {x: 1, y: 2}];
let boo = false;
for (let i = 0; i < bombs.length && !boo; i++) {
for (let j = 0; j < bombs.length; j++) {
if (i === j) continue; // this is where you don't compare the object with itself
if (bombs[i].x === bombs[j].x && bombs[i].y === bombs[j].y) {
boo = true;
break;
}
}
}