Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

166
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda