Skip to content
Advertisement

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

Advertisement

Answer

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; }
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement