Skip to content
Advertisement

How can I group the nested array according to the machineGroupNo field and add the total capacities according to this grouping?

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

Advertisement

Answer

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);
Advertisement