Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

165
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda