-
Notifications
You must be signed in to change notification settings - Fork 664
How to group arrays by multiple fields
Mathias Rangel Wulff edited this page Jun 13, 2015
·
2 revisions
Source: StackOverflow.com
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}
];
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.
© 2014-2024, Andrey Gershun & Mathias Rangel Wulff
Please help improve the documentation by opening a PR on the wiki repo