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

119
Views
Reemplace un objeto que está dentro de la matriz si el valor de identificación del objeto de otra matriz coincide

Tengo estas matrices con objetos que se ven así:

 array1 = [ 0:{id:145, value:130000}, 1:{id:146, value:103300}, 2:{id:147, value:79500}, ] array2 = [ 0:{id:145, value:135000} ]

Y quiero reemplazar el objeto dentro de la matriz si la identificación del objeto en la array2 coincide con alguna identificación del objeto en la array1

Así que espero algo como esto:

 array1 = [ 0:{id:145, value:135000}, 1:{id:146, value:103300}, 2:{id:147, value:79500}, ]

tengo este codigo

 array1.splice(1, 1, array2[0])

pero me devuelve esto:

 array1 = [ 0:{id:145, value:135000}, 1:{id:145, value:130000}, 2:{id:146, value:103300}, 3:{id:147, value:79500}, ]

Cualquier ayuda la agradeceré

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

0

array2.forEach(i1 => { const index = array1.findIndex(i2 => i2.id == i1.id); if(index > -1) { array1.splice(index, 1, i1); } });
about 4 years ago · Juan Pablo Isaza Report

0

 let array1 = [ {id:145, value:130000}, {id:146, value:103300}, {id:147, value:79500}, ] let array2 = [ {id:145, value:135000}, {id:147, value:135023} ] array2.map(x => { let index = array1.findIndex(d=> d.id === x.id) array1[index] = x }) console.log(array1)

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!