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

216
Views
Combinar elementos de matriz agrupados por propiedades en Javascript

Tengo la siguiente matriz de objetos javascript:

 [ { "firstName": "x", "lastName": "y", "age": 10}, { "firstName": "x", "lastName": "y", "height": 100}, { "firstName": "x", "lastName": "y", "weight": 50}, { "firstName": "a", "lastName": "b", "age": 11}, { "firstName": "a", "lastName": "b", "height": 110}, { "firstName": "a", "lastName": "b", "weight": 60}, { "firstName": "m", "lastName": "n", "age": 12}, { "firstName": "m", "lastName": "n", "height": 120}, { "firstName": "m", "lastName": "n", "weight": 70}]

¿Es posible agrupar por nombre y apellido de modo que incluya otras propiedades del objeto en la matriz?

Matriz esperada:

 [ { "firstName": "x", "lastName": "y", "age": 10, "height": 100, "weight": 50}, { "firstName": "a", "lastName": "b", "age": 11, "height": 110, "weight": 60}, { "firstName": "m", "lastName": "n", "age": 12, "height": 120, "weight": 70}]
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Concepto

Prepare una matriz de resultados. Iterar a través de todos los datos. Compruebe si el firstName y el lastName existen en la matriz de resultados. Si no, introdúzcalo en la matriz de resultados. En caso afirmativo, combine el objeto para obtener las propiedades que faltan.

Código

 const data =[ { "firstName": "x", "lastName": "y", "age": 10}, { "firstName": "x", "lastName": "y", "height": 100}, { "firstName": "x", "lastName": "y", "weight": 50}, { "firstName": "a", "lastName": "b", "age": 11}, { "firstName": "a", "lastName": "b", "height": 110}, { "firstName": "a", "lastName": "b", "weight": 60}, { "firstName": "m", "lastName": "n", "age": 12}, { "firstName": "m", "lastName": "n", "height": 120}, { "firstName": "m", "lastName": "n", "weight": 70}]; let result = []; let found; data.forEach(d => { found = false; result.forEach(r => { if (r.firstName === d.firstName && r.lastName === d.lastName){ Object.assign(r, d); found = true; } }); if (!found){ result.push(d); } }); console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

puedes hacer algo como esto

 const group = data => Object.values(data.reduce((res, {firstName, lastName, ...rest}) =>{ const key = JSON.stringify({firstName, lastName}) return { ...res, [key]: {firstName, lastName, ...(res[key] || {}), ...rest} } }, {})) const data = [ { "firstName": "x", "lastName": "y", "age": 10}, { "firstName": "x", "lastName": "y", "height": 100}, { "firstName": "x", "lastName": "y", "weight": 50}, { "firstName": "a", "lastName": "b", "age": 11}, { "firstName": "a", "lastName": "b", "height": 110}, { "firstName": "a", "lastName": "b", "weight": 60}, { "firstName": "m", "lastName": "n", "age": 12}, { "firstName": "m", "lastName": "n", "height": 120}, { "firstName": "m", "lastName": "n", "weight": 70}] console.log(group(data))

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!