group all items with same name js

Solutions on MaxInterview for group all items with same name js by the best coders in the world

showing results for - "group all items with same name js"
Raphael
12 May 2016
1const inputArray = [ 
2    { Phase: "Phase 1", Step: "Step 1", Task: "Task 1", Value: "5" },
3    { Phase: "Phase 1", Step: "Step 1", Task: "Task 2", Value: "10" },
4    { Phase: "Phase 1", Step: "Step 2", Task: "Task 1", Value: "15" },
5    { Phase: "Phase 1", Step: "Step 2", Task: "Task 2", Value: "20" },
6    { Phase: "Phase 2", Step: "Step 1", Task: "Task 1", Value: "25" },
7    { Phase: "Phase 2", Step: "Step 1", Task: "Task 2", Value: "30" },
8    { Phase: "Phase 2", Step: "Step 2", Task: "Task 1", Value: "35" },
9    { Phase: "Phase 2", Step: "Step 2", Task: "Task 2", Value: "40" }
10];
11
12var outObject = inputArray.reduce(function(a, e) {
13  // GROUP BY estimated key (estKey), well, may be a just plain key
14  // a -- Accumulator result object
15  // e -- sequentally checked Element, the Element that is tested just at this itaration
16
17  // new grouping name may be calculated, but must be based on real value of real field
18  let estKey = (e['Phase']); 
19
20  (a[estKey] ? a[estKey] : (a[estKey] = null || [])).push(e);
21  return a;
22}, {});
23
24console.log(outObject);