I have nested array fields. I want to group according to the machineGroupNo fields in these arrays and group all the total capacities in these machineGroup fields into that machineGroup.
My Arrays are like this
0: (4) [{…}, {…}, {…}, {…}]
1: (4) [{…}, {…}, {…}, {…}]
2: (4) [{…}, {…}, {…}, {…}]
3: (4) [{…}, {…}, {…}, {…}]
0: Array(4)
0: { MachineGroupNo: '55003000', TotalCapacity: 0, TotalGroupCompanyCapacity: 0, UsedTotalCapacity: 5496, …}
1: { MachineGroupNo: '55006000', TotalCapacity: 0, TotalGroupCompanyCapacity: 0, UsedTotalCapacity: 5496, …}
2: { MachineGroupNo: '55007000', TotalCapacity: 0, TotalGroupCompanyCapacity: 0, UsedTotalCapacity: 9978, …}
3: { MachineGroupNo: '55014000', TotalCapacity: 0, TotalGroupCompanyCapacity: 0, UsedTotalCapacity: 5496, …}
1: Array(4)
0: {MachineGroupNo: '55003000', TotalCapacity: 1000, TotalGroupCompanyCapacity: 500, UsedTotalCapacity: 200, …}
1: {MachineGroupNo: '55006000', TotalCapacity: 800, TotalGroupCompanyCapacity: 600, UsedTotalCapacity: 200, …}
2: {MachineGroupNo: '55007000', TotalCapacity: 1600, TotalGroupCompanyCapacity: 1200, UsedTotalCapacity: 146, …}
3: {MachineGroupNo: '55014000', TotalCapacity: 750, TotalGroupCompanyCapacity: 350, UsedTotalCapacity: 200, …}
2: (4) [{…}, {…}, {…}, {…}]
3: (4) [{…}, {…}, {…}, {…}]
I have arrays nested in this way. After entering each array and grouping it according to the machineGroupNo here, I want it to sum all the fields in this machineGroupNo. For example, let it find all machineGroupNo's in the machineGroupNo field with the number "55000010" and add the "UsedTotalCapacity" fields. Likewise, I want it to add and add the other capacity fields as well.
[
0: {MachineGroupNo: '55003000', TotalCapacity: 1000, TotalGroupCompanyCapacity: 500, UsedTotalCapacity: 5696, …}
1: {MachineGroupNo: '55006000', TotalCapacity: 800, TotalGroupCompanyCapacity: 600, UsedTotalCapacity: 5696, …}
2: { MachineGroupNo: '55007000', TotalCapacity: 1600, TotalGroupCompanyCapacity: 1200, UsedTotalCapacity: 10124, …}
3: { MachineGroupNo: '55014000', TotalCapacity: 750, TotalGroupCompanyCapacity: 350, UsedTotalCapacity: 5696, …}
]
i want to see it like above
CodePudding user response:
const data = [
[
{ MachineGroupNo: "55003000", TotalCapacity: 1, TotalGroupCompanyCapacity: 2, UsedTotalCapacity: 5496 },
{ MachineGroupNo: "55006000", TotalCapacity: 3, TotalGroupCompanyCapacity: 4, UsedTotalCapacity: 5496 },
{ MachineGroupNo: "55007000", TotalCapacity: 5, TotalGroupCompanyCapacity: 6, UsedTotalCapacity: 9978 },
{ MachineGroupNo: "55014000", TotalCapacity: 7, TotalGroupCompanyCapacity: 8, UsedTotalCapacity: 5496 }
],
[
{ MachineGroupNo: "55003000", TotalCapacity: 1000, TotalGroupCompanyCapacity: 500, UsedTotalCapacity: 200 },
{ MachineGroupNo: "55006000", TotalCapacity: 800, TotalGroupCompanyCapacity: 600, UsedTotalCapacity: 200 },
{ MachineGroupNo: "55007000", TotalCapacity: 1600, TotalGroupCompanyCapacity: 1200, UsedTotalCapacity: 146 },
{ MachineGroupNo: "55014000", TotalCapacity: 750, TotalGroupCompanyCapacity: 350, UsedTotalCapacity: 200 }
]
];
const result = [];
const idName = "MachineGroupNo";
data.forEach(groupArr => {
groupArr.forEach(group => {
const id = group[idName];
let resGroup = result.find(g => g[idName] === id);
if (!resGroup) {
resGroup = { [idName]: id };
result.push(resGroup);
}
Object.keys(group)
.filter(k => k !== idName)
.forEach(k => resGroup[k] = (resGroup[k] || 0) group[k]);
});
});
console.log(result);
CodePudding user response:
First of all, flat your array. Then, since you are grouping, calculate the sums of your desired fields:
const dataSource = [
[
{ MachineGroupNo: '55003000', TotalCapacity: 0, TotalGroupCompanyCapacity: 0, UsedTotalCapacity: 5496 },
{ MachineGroupNo: '55006000', TotalCapacity: 0, TotalGroupCompanyCapacity: 0, UsedTotalCapacity: 5496 },
{ MachineGroupNo: '55007000', TotalCapacity: 0, TotalGroupCompanyCapacity: 0, UsedTotalCapacity: 9978 },
{ MachineGroupNo: '55014000', TotalCapacity: 0, TotalGroupCompanyCapacity: 0, UsedTotalCapacity: 5496 },
],
[
{ MachineGroupNo: '55003000', TotalCapacity: 0, TotalGroupCompanyCapacity: 0, UsedTotalCapacity: 5496 },
{ MachineGroupNo: '55006000', TotalCapacity: 0, TotalGroupCompanyCapacity: 0, UsedTotalCapacity: 5496 },
{ MachineGroupNo: '55007000', TotalCapacity: 0, TotalGroupCompanyCapacity: 0, UsedTotalCapacity: 9978 },
{ MachineGroupNo: '55014000', TotalCapacity: 0, TotalGroupCompanyCapacity: 0, UsedTotalCapacity: 5496 },
],
];
const groups = dataSource
.flat()
.reduce((state, x) => {
const key = x.MachineGroupNo;
// state[key] ??= { items: [], sumUsedTotalCapacity: 0 }; // NodeJs 16.x
state[key] = state[key] || { items: [], sumUsedTotalCapacity: 0 }; // all versions
state[key].items.push(x);
state[key].sumUsedTotalCapacity = x.UsedTotalCapacity;
return state;
}, {});
console.log(groups)
It computes groups in O(n), regardless of flatting. The output should be:
{
55003000: {
sumUsedTotalCapacity: 10992,
items: [
{ MachineGroupNo: '55003000', TotalCapacity: 0, TotalGroupCompanyCapacity: 0, UsedTotalCapacity: 5496,},
{ MachineGroupNo: '55003000', TotalCapacity: 0, TotalGroupCompanyCapacity: 0, UsedTotalCapacity: 5496, },
],
},
....
}
Feel free to add other sum operations.