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

163
Views
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 answers
Answer question

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 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!