I would like to compare the two arrangements of the following script to delete the duplicate arrangement.
I used the Filter and Includes function or for statement but failed.
What should I do?
let ground = [
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,3,3,7,7,7,7,8,8,4,4,4,2,2,3,7,1],
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
]
let deleteBlock = [[1,3,3,7,7,7,7,8,8,4,4,4,2,2,3,7,1]]
I tried in this way
let ans = ground.filter((r,idx) => {
for(let i =0; i < deleteBlock.length;i++) {
if(r === deleteBlock[i]) {
ground.splice(idx, 1)
}
}
})
ground.filter((r) => deleteBlock.forEach(ele=> r.includes(ele)))
If you compare two arrays with the same properties you will always get false. For example, [1] === [1] always returns false since they are each a brand new array object with just identical contents.
So you should loop through both arrays and compare each of their properties one by one or in your case since I assume type is not essential you could deconstruct them and then compare the strings like this:
const filtered = ground.filter((row) => { return row.join() !== deleteBlock.join(); });
You could create an arrayEqual function that compares two arrays
function arrayEqual(arr1, arr2) {
if (arr1.length !== arr2.length) return false;
for (let i = 0; i < arr1.length; i++)
if (arr1[i] !== arr2[i]) return false; //adapt equality test if you have complex objects
return true;
}
and then use it like the follows
let ground = [...];
let filtered = ground.filter(x => deleteBlock.every(y => !arrayEqual(x,y)));