Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

166
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda