Most efficient method to groupby on an array of objects - JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

Suppose, we have an array of objects like this −

const arr = [
   { Phase: "Phase 1", Step: "Step 1", Task: "Task 1", Value: "5" },
   { Phase: "Phase 1", Step: "Step 1", Task: "Task 2", Value: "10" },
   { Phase: "Phase 1", Step: "Step 2", Task: "Task 1", Value: "15" },
   { Phase: "Phase 1", Step: "Step 2", Task: "Task 2", Value: "20" },
   { Phase: "Phase 2", Step: "Step 1", Task: "Task 1", Value: "25" },
   { Phase: "Phase 2", Step: "Step 1", Task: "Task 2", Value: "30" },
   { Phase: "Phase 2", Step: "Step 2", Task: "Task 1", Value: "35" },
   { Phase: "Phase 2", Step: "Step 2", Task: "Task 2", Value: "40" }
];

We are required to write a JavaScript function that takes in one such array as the first argument.

Our purpose is to be able to group the given array based on multiple or one single property (indicated by second, third arguments.)

Therefore, if we did groupby Phase, we should receive −

const output = [
   { Phase: "Phase 1", Value: 50 },
   { Phase: "Phase 2", Value: 130 }
];

And if we did groupy Phase / Step, we should receive −

const output = [
   { Phase: "Phase 1", Step: "Step 1", Value: 15 },
   { Phase: "Phase 1", Step: "Step 2", Value: 35 },
   { Phase: "Phase 2", Step: "Step 1", Value: 55 },
   { Phase: "Phase 2", Step: "Step 2", Value: 75 }
];

Example

The code for this will be −

const arr = [
   { Phase: "Phase 1", Step: "Step 1", Task: "Task 1", Value: "5" },
   { Phase: "Phase 1", Step: "Step 1", Task: "Task 2", Value: "10" },
   { Phase: "Phase 1", Step: "Step 2", Task: "Task 1", Value: "15" },
   { Phase: "Phase 1", Step: "Step 2", Task: "Task 2", Value: "20" },
   { Phase: "Phase 2", Step: "Step 1", Task: "Task 1", Value: "25" },
   { Phase: "Phase 2", Step: "Step 1", Task: "Task 2", Value: "30" },
   { Phase: "Phase 2", Step: "Step 2", Task: "Task 1", Value: "35" },
   { Phase: "Phase 2", Step: "Step 2", Task: "Task 2", Value: "40" }
];
const groupBy = (array, groups, valueKey) => {
   const map = new Map;
   groups = [].concat(groups);
   return array.reduce((acc, val) => {
      groups.reduce((accu, value, ind, { length }) => {
         let child;
         if (accu.has(val[value])) {
            return accu.get(val[value]);
         };
         if (ind + 1 === length) {
            child = Object
            .assign(...groups.map(value => ({ [value]: val[value]
            })), { [valueKey]: 0 });
            acc.push(child);
         }
         else {
            child = new Map;
         };
         accu.set(val[value], child);
         return child;
      }, map)[valueKey] += +val[valueKey];
      return acc;
   }, []);
};
console.log(groupBy(arr, 'Phase', 'Value'));
console.log(groupBy(arr, ['Phase', 'Step'], 'Value'));

Output

And the output in the console will be −

[ { Phase: 'Phase 1', Value: 50 }, { Phase: 'Phase 2', Value: 130 } ]
[
   { Phase: 'Phase 1', Step: 'Step 1', Value: 15 },
   { Phase: 'Phase 1', Step: 'Step 2', Value: 35 },
   { Phase: 'Phase 2', Step: 'Step 1', Value: 55 },
   { Phase: 'Phase 2', Step: 'Step 2', Value: 75 }
]
raja
Published on 24-Nov-2020 10:44:25
Advertisements