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

143
Views
¿Cómo puedo eliminar un elemento de una matriz y enviar un resultado?

Tengo esta matriz:

 [ { "id": "z12", "val": "lu", "val2": "1", }, { "id": "z13", "val": "la", "val2": "2", }, { "id": "z14", "val": "lo", "val2": "3", }, ]

tengo segundo

 array2 = { tab: [], }

lo hago en mi mecanografiado:

 array.forEach((item, index)=>{ if(item.id === z12){ array.splice(index, 1); }else{ array2.tab.push(item.id); } });

Normalmente debería tener: tab: [z13,z14] pero no funciona

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Puede filtrar y mapear directamente.

 const array = [{ "id": "z12", "val": "lu", "val2": "1", }, { "id": "z13", "val": "la", "val2": "2", }, { "id": "z14", "val": "lo", "val2": "3", },]; const array2 = { tab: array.filter(e => e['id'] != "z12").map(e => e['id']), } console.log(array2)

about 4 years ago · Juan Pablo Isaza Report

0

Creo que podrías hacerlo en dos pasos:

 // first remove the "z12" from the array as you requested array = array.filter(e => e.id !== 'z12') // and second get the remaining ids array2 = { tab: array.map(e => e.id) }
about 4 years ago · Juan Pablo Isaza Report

0

El método splice () en forEach () causa el problema cuando se eliminó el "z12", la matriz se convirtió en

 [ { "id": "z13", "val": "la", "val2": "2", }, { "id": "z14", "val": "lo", "val2": "3", }, ]

PERO la iteración del método forEach no se detuvo. continuará con el índice de 1 que justo después de que se eliminó el z12, por lo que el índice de 1 es el "z14", no el "z13".

¡algo de conocimiento de forEach forEach es MALO!

about 4 years ago · Juan Pablo Isaza 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!