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

105
Views
Modifique los objetos en la matriz de resultados al combinar dos matrices en una

Tengo dos matrices de objetos (Array1 y Array2) que debo combinar en una matriz (resultado).

Si un valor está presente en ambas matrices, debe estar solo una vez en la matriz final.

Si un valor no está presente en ninguna de las matrices, debe agregarse con el booleano establecido en verdadero.

Aquí está la situación:

 const Array1 = [ {value: "id1", name: "Element1", boolean: false}, {value: "id2", name: "Element2", boolean: false}, {value: "id3", name: "Element3", boolean: false} ] const Array2 = [ {value: "id1", name: "Element1", boolean: false}, {value: "id2", name: "Element2", boolean: false}, {value: "id4", name: "Element4", boolean: false} ]

Rendimiento esperado:

 const result = [ {value: "id1", name: "Element1", boolean: false}, {value: "id2", name: "Element2", boolean: false}, {value: "id3", name: "Element3", boolean: true}, {value: "id4", name: "Element4", boolean: true} ]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puede lograr fácilmente el resultado usando Map

 const Array1 = [ { value: "id1", name: "Element1", boolean: false }, { value: "id2", name: "Element2", boolean: false }, { value: "id3", name: "Element3", boolean: false }, ]; const Array2 = [ { value: "id1", name: "Element1", boolean: false }, { value: "id2", name: "Element2", boolean: false }, { value: "id4", name: "Element4", boolean: false }, ]; const map = new Map(Array1.map((o) => [o.value, [o]])); Array2.forEach((o) => map.has(o.value) ? map.get(o.value).push(o) : map.set(o.value, [o]) ); const result = [...map.values()].map((arr) => arr.length > 1 ? arr[0] : { ...arr[0], boolean: true } ); console.log(result);
 /* This is not a part of answer. It is just to give the output full height. So IGNORE IT */ .as-console-wrapper { max-height: 100% !important; top: 0; }

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!