简体   繁体   English

按 id 合并数组中的多个对象 - javascript

[英]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.我有一组可以具有重复 ID 的对象。 There are 3 objects for id 'THOM01': id 'THOM01' 有 3 个对象:

[
    { 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).需要合并具有相同 ID 的对象(每个员工一个 object)。 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.当我输出到 csv 以上传到工资单系统时,我需要维护键/值对(包括空值对)的结构/位置,但对象的顺序无关紧要。

The other solutions I've tried always end up with only one value for the payrates ('T1' to 'other').我尝试过的其他解决方案最终总是只有一个支付率值(“T1”到“其他”)。 They seem to be getting overwritten as each object has all the fields, even if they're empty.它们似乎被覆盖了,因为每个 object 都有所有字段,即使它们是空的。

I hope that's enough info.我希望这是足够的信息。 Thanks!谢谢!

Dave戴夫

You could take an array of keys and reduce the data by taking an object for grouping.您可以采用一组键并通过采用 object 进行分组来减少数据。

 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; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM