I have a json array object . I want to group by data based on column "FlexCategoryName" and perform aggregation on matrix columns such as avg of "cost" ,sum of "clicks" etc . Below are JSON array Object
Array[100]
0: Object
1: Object
2: Object
3: Object
0: Object
Clicks: 1067
Cost: 4094.2
Date: "2/8/2014"
FlexCategoryName: "Cat1"
Impressions: 65943
Leads: 20
VENUE_ID:1
1: Object
Clicks: 106
Cost: 409
Date: "2/8/2014"
FlexCategoryName: "Cat2"
Impressions: 65941
Leads: 21
VENUE_ID:1
2: Object
Clicks: 106
Cost: 409
Date: "2/8/2014"
FlexCategoryName: "Cat2"
Impressions: 10
Leads: 21
VENUE_ID:2
3: Object
Clicks: 106
Cost: 409
Date: "2/8/2014"
FlexCategoryName: "Cat3"
Impressions: 10
Leads: 21
VENUE_ID:2
4: Object
Clicks: 20
Cost: 10
Date: "2/8/2014"
FlexCategoryName: "Cat5"
Impressions: 10
Leads: 21
VENUE_ID:3
How to use Underscore JS to perform group by and aggregation on matrix columns .
Expected Output
0: Object
Cost: sum of all cost based on FlexCategoryName Cat1
FlexCategoryName: "Cat1"
Impressions: sum of all Impressions based on FlexCategoryName Cat1
1: Object
Cost: sum of all cost based on FlexCategoryName Cat2
FlexCategoryName: "Cat2"
Impressions: sum of all Impressions based on FlexCategoryName Cat2
Output
If you want to find average values instead of sum, then just change the function.
Edit: If you want to group based on more than one fields, you can nest the grouping like this