Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most efficient method to groupby on an array of objects

What is the most efficient way to groupby objects in an array?

For example, given this array of objects:

[      { 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" } ] 

I’m displaying this information in a table. I’d like to groupby different methods, but I want to sum the values.

I’m using Underscore.js for its groupby function, which is helpful, but doesn’t do the whole trick, because I don’t want them “split up” but “merged”, more like the SQL group by method.

What I’m looking for would be able to total specific values (if requested).

So if I did groupby Phase, I’d want to receive:

[     { Phase: "Phase 1", Value: 50 },     { Phase: "Phase 2", Value: 130 } ] 

And if I did groupy Phase / Step, I’d receive:

[     { 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 } ] 

Is there a helpful script for this, or should I stick to using Underscore.js, and then looping through the resulting object to do the totals myself?

like image 384
D'Arcy Rail-Ip Avatar asked Jan 21 '13 20:01

D'Arcy Rail-Ip


People also ask

How can I group an array of objects?

The most efficient way to group an array of objects in JavaScript is to use the reduce() function. This method executes each array element's (specified) reducer function and produces a single output value. Example: We have a product list with the two properties, name and category.

How do you use reduce on array of objects in JavaScript?

The array reduce in JavaScript is a predefined method used to reduce an array to a single value by passing a callback function on each element of the array. It accepts a function executed on all the items of the specified array in the left-to-right sequence. The returned single value is stored in the accumulator.

How do you turn an object into an array?

To convert an object to an array you use one of three methods: Object. keys() , Object. values() , and Object. entries() .

How do you use reduce in js?

JavaScript Array reduce()The reduce() method executes a reducer function for array element. The reduce() method returns a single value: the function's accumulated result. The reduce() method does not execute the function for empty array elements. The reduce() method does not change the original array.


1 Answers

If you want to avoid external libraries, you can concisely implement a vanilla version of groupBy() like so:

var groupBy = function(xs, key) {   return xs.reduce(function(rv, x) {     (rv[x[key]] = rv[x[key]] || []).push(x);     return rv;   }, {}); };  console.log(groupBy(['one', 'two', 'three'], 'length'));  // => {3: ["one", "two"], 5: ["three"]}
like image 134
Ceasar Bautista Avatar answered Sep 17 '22 17:09

Ceasar Bautista