Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

163
Views
Javascript) I would like to compare two two-dimensional arrays to delete the duplicate elements

As I have already asked, I would like to ask again because I wanted a mutable method, not an immutable method.
I want to compare the two arrays below to delete the duplicate elements.
After deleting the duplicate elements, I want to delete it from the existing ground array, rather than creating a new array.

I approached this way, but it doesn't seem to work properly.

   const filtered = ground.filter((row, idx) => {
            if (row.join() === deleteBlock.join()) {
              return ground.splice(idx, 1);
            }
          });

let ground = [
  
  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
  [4,3,2,3,4,5,6,7,8,6,5,5,3,2,3],
  [2,2,2,2,2,2,2,2,2,3,3,3,3,4,5],
  [3,3,7,7,7,7,8,8,4,4,4,2,2,3,7]
]

let deleteBlock = [
  [4,3,2,3,4,5,6,7,8,6,5,5,3,2,3],
  [2,2,2,2,2,2,2,2,2,3,3,3,3,4,5],
  [3,3,7,7,7,7,8,8,4,4,4,2,2,3,7]
]

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

So one issue is that when you splice the ground array you are removing the element and then the .forEach method jumps FORWARD to the next element and now you've skipped over an element. If you use a traditional for loop you can shift the for loop back one every time you remove an element so you do not skip any.

Also, you seem to be comparing all of deleteBlock to each row of ground rather than comparing each row of deleteBlock to each row of ground. So I added an additional forEach inside each iteration of the for loop.

for(let x=0;x<ground.length;x++){
    deleteBlock.forEach((dBlock)=>{
    //check each row to each row
    if (ground[x].join() === dBlock.join()) {
        console.log(dBlock)
        ground.splice(x, 1);
        //after removing an item shift the for loop back one to avoid skipping
        x--
    }
  })
}
about 4 years ago · Santiago Gelvez Report

0

/*
* There are many ways to reach this solution
* This is my bet
*/
    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,4,3,2,3,4,5,6,7,8,6,5,5,3,2,3,1],
      [1,2,2,2,2,2,2,2,2,2,3,3,3,3,4,5,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,4,3,2,3,4,5,6,7,8,6,5,5,3,2,3,1],
      [1,2,2,2,2,2,2,2,2,2,3,3,3,3,4,5,1],
      [1,3,3,7,7,7,7,8,8,4,4,4,2,2,3,7,1]
    ];

/* 
* I prefer use JSON.stringify to be more confident then using Array.join()

deleteBlock = deleteBlock.map(m=>JSON.stringify(m));
newGround = ground.filter(item=>(!deleteBlock.includes(JSON.stringify(item)))); 
 

console.log(newGround);
*/

/* 
* No Array Copy
* for this, you need to pass to Array.splice() the length of the deleteBlock
*/
deleteBlock = deleteBlock.map(m=>JSON.stringify(m));
ground.forEach((item,i)=>deleteBlock.includes(JSON.stringify(item)) ? ground.splice(i,deleteBlock.length) : null);  

console.log(ground);

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!