我对以下代码有疑问。它旨在从格式化为的对象数组中删除重复项(第三个和最后一个条目)
{x: val, y: val2}
但是,我偶尔会得到带有重复(图像)的输出,例如:
Object {x: 5, y: 0}
Object {x: 7, y: 0}
Object {x: 7, y: 5}
Object {x: 5, y: 2}
Object {x: 2, y: 1}
Object {x: 7, y: 5}
为什么这段代码没有按预期删除重复项?
var mines = [{}];
console.log("\n\n\n\n\n");
for (var i = 0; i < 10; i++){ //for each mine that needs to be made
var mine = {}; //initalize temporary object
var dupeerr = false; //set duplication error flag to false
//(will be flipped if loop needs to be re-executed)
do{
//generate coordinates for new mine
//(random int between 0 and max size), then rounded to
mine.x = Math.round(Math.random()*(8));
mine.y = Math.round(Math.random()*(9));
for (var j = mines.length - 1; j >= 0; j--) { //for each mine in array
if ((mines[j].x == mine.x) && (mines[j].y == mine.y)) { //check for doubles
dupeerr = true; //flag for re-execution
} else if (mines[j] != mine) {
dupeerr = false;
}
};
//console.log(mines);
} while (dupeerr || bounderr);
mines[i] = mine;
}
var ans = [];
for (var i = 0; i < mines.length; i++) {
ans[i] = mines[i];
var minebtn = document.getElementById(JSON.stringify(ans[i]));
console.log(ans[i]);
};