Skip to content

How to group arrays by multiple fields

Mathias Rangel Wulff edited this page Jun 13, 2015 · 2 revisions

How to group arrays by multiple fields?

Source: StackOverflow.com

Question

How to get a new JSON object from this:

`    (group by "a" & group by "b" field with sum "d" and with count(objects))`
    var json = [
        {"a":121, "b":212, "c":"0", "d":100},
        {"a":121, "b":212, "c":"0", "d":300},
        {"a":121, "b":210, "c":"0", "d":200},
        {"a":120, "b":210, "c":"0", "d":300}
        ];
        
    var new_json = [
        {"a":121, "b":212, "c":"0", "d":400, "count":2},
        {"a":121, "b":210, "c":"0", "d":200, "count":1},
        {"a":120, "b":210, "c":"0", "d":300, "count":1}
        ];

Answer

You can do it with AlaSQL library:

var res = alasql('SELECT a,b,c,SUM(d) AS d,COUNT(*) AS [count] FROM ? \
                  GROUP BY a,b,c',[json]);

Try this example at jsFiddle.

Clone this wiki locally