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

113
Views
JS: aplanar y reducir la matriz de objetos con múltiples propiedades y combinar valores

He estado atascado en esto por un tiempo, pero no estoy seguro de cómo puedo aplanar y reducir los datos de ejemplo a continuación en pares de valores clave únicos con el valor combinado si hay múltiplos de la misma clave.

Ejemplo:

 const data = [ { test: 200 }, { test2: 300, test3: 100 }, { test3: 400, test4: 150, test2: 50 }, ];

Resultado Esperado:

 const result = [ { test: 200 }, { test2: 350 }, { test3: 500 }, { test4: 150 } ];

¿Algunas ideas?

AIT

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

0

Prueba esto:

 const data = [ { test: 200 }, { test2: 300, test3: 100 }, { test3: 400, test4: 150, test2: 50 } ]; const flattenedAndReduced = data.reduce((prev, current) => { Object.keys(prev).forEach((key, index) => { if (current[key]) { prev[key] += current[key]; delete current[key]; } }); Object.keys(current).forEach((key) => { if (!prev[key]) prev[key] = current[key]; }); return { ...prev }; }); const flattenedAndReducedToArray = Object.keys(flattenedAndReduced).map((key) => ({ [key]: flattenedAndReduced[key] }));
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!