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

182
Views
Javascript) Me gustaría comparar dos matrices bidimensionales para eliminar los elementos duplicados

Como ya pregunté, me gustaría volver a preguntar porque quería un método mutable, no un método inmutable.
Quiero comparar las dos matrices a continuación para eliminar los elementos duplicados.
Después de eliminar los elementos duplicados, quiero eliminarlos de la matriz de suelo existente , en lugar de crear una nueva matriz.

Me acerqué de esta manera, pero no parece funcionar correctamente.

 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

Entonces, un problema es que cuando empalma la matriz de tierra, está eliminando el elemento y luego el método .forEach salta ADELANTE al siguiente elemento y ahora se ha saltado un elemento. Si usa un bucle for tradicional, puede cambiar el bucle for hacia atrás cada vez que elimine un elemento para no omitir ninguno.

Además, parece que está comparando todo deleteBlock con cada fila de suelo en lugar de comparar cada fila de deleteBlock con cada fila de ground . Así que agregué un forEach adicional dentro de cada iteración del bucle for.

 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,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,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!