Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

167
Vistas
Merge multiple objects in array by id - javascript

This is my first question, so please be gentle :)

I know my question is similar to many others, and I have tried a LOT of solutions, but I'm not getting the result I need.

I have an array of objects that can have duplicate id's. There are 3 objects for id 'THOM01':

[
    { id: 'MARS03', name: 'Employee 3', T1: 9.23, A6: '', SAT: '', SUN: '', PHW: '', SAN: '', COMM:'', BON: '', other: '', quantity: '', leaveDays: '', from: '', to: '', reason: '', costCentre: '', message: '' },
    { id: 'THOM01', name: 'Employee 4', T1: '', A6: '', SAT: 4.12, SUN: '', PHW: '', SAN: '', COMM: '', BON: '', other: '', quantity: '', leaveDays: '', from: '', to: '', reason: '', costCentre: '', message: '' },
    { id: 'THOM01', name: 'Employee 4', T1: '', A6: '', SAT: '', SUN: 6.12, PHW: '', SAN: '', COMM: '', BON: '', other: '', quantity: '', leaveDays: '', from: '', to: '', reason: '', costCentre: '', message: '' },
    { id: 'THOM01', name: 'Employee 4', T1: 15, A6: '', SAT: '', SUN: '', PHW: '', SAN: '', COMM: '', BON: '', other: '', quantity: '', leaveDays: '', from: '', to: '', reason: '', costCentre: '', message: '' }
]

The objects with the same id's need to be merged (one object per employee). So the result would look like this:

[
    { id: 'MARS03', name: 'Employee 3', T1: 9.23, A6: '', SAT: '', SUN: '', PHW: '', SAN: '', COMM:'', BON: '', other: '', quantity: '', leaveDays: '', from: '', to: '', reason: '', costCentre: '', message: '' },
    { id: 'THOM01', name: 'Employee 4', T1: 15, A6: 4.12, SAT: 4.12, SUN: '', PHW: '', SAN: '', COMM: '', BON: '', other: '', quantity: '', leaveDays: '', from: '', to: '', reason: '', costCentre: '', message: '' }
]

As I'm outputting to csv to upload to a payroll system, I need to maintain the structure/placement of the key/value pairs (including the empty value pairs), but the order of the objects doesn't matter.

The other solutions I've tried always end up with only one value for the payrates ('T1' to 'other'). They seem to be getting overwritten as each object has all the fields, even if they're empty.

I hope that's enough info. Thanks!

Dave

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You could take an array of keys and reduce the data by taking an object for grouping.

const
    data = [{ id: 'MARS03', name: 'Employee 3', T1: 9.23, A6: '', SAT: '', SUN: '', PHW: '', SAN: '', COMM:'', BON: '', other: '', quantity: '', leaveDays: '', from: '', to: '', reason: '', costCentre: '', message: '' }, { id: 'THOM01', name: 'Employee 4', T1: '', A6: '', SAT: 4.12, SUN: '', PHW: '', SAN: '', COMM: '', BON: '', other: '', quantity: '', leaveDays: '', from: '', to: '', reason: '', costCentre: '', message: '' }, { id: 'THOM01', name: 'Employee 4', T1: '', A6: '', SAT: '', SUN: 6.12, PHW: '', SAN: '', COMM: '', BON: '', other: '', quantity: '', leaveDays: '', from: '', to: '', reason: '', costCentre: '', message: '' }, { id: 'THOM01', name: 'Employee 4', T1: 15, A6: '', SAT: '', SUN: '', PHW: '', SAN: '', COMM: '', BON: '', other: '', quantity: '', leaveDays: '', from: '', to: '', reason: '', costCentre: '', message: '' }],
    keys = ['T1', 'A6', 'SAT', 'SUN', 'PHW', 'SAN', 'COMM', 'BON', 'other'],
    result = Object.values(data.reduce((r, o) => {
        if (r[o.id]) keys.forEach(k => { if (o[k] !== '') r[o.id][k] = (r[o.id][k] || 0) + o[k]; });
        else r[o.id] = { ...o };
        return r;
    }, {}));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda