Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Group by and aggregation on JSON array using Underscore JS

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
like image 424
Shailesh Avatar asked Apr 09 '14 06:04

Shailesh


1 Answers

var _ = require("underscore");
function sum(numbers) {
    return _.reduce(numbers, function(result, current) {
        return result + parseFloat(current);
    }, 0);
}
var result = _.chain(data)
    .groupBy("FlexCategoryName")
    .map(function(value, key) {
        return {
            FlexCategoryName: key,
            Cost: sum(_.pluck(value, "Cost")),
            Impressions: sum(_.pluck(value, "Impressions"))
        }
    })
    .value();

console.log(result);

Output

[ { FlexCategoryName: 'Cat1', Cost: 4094.2, Impressions: 65943 },
  { FlexCategoryName: 'Cat2', Cost: 818, Impressions: 65951 },
  { FlexCategoryName: 'Cat3', Cost: 409, Impressions: 10 },
  { FlexCategoryName: 'Cat5', Cost: 10, Impressions: 10 } ]

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

var result = _.chain(data)
    .groupBy("FlexCategoryName")
    .map(function(value, key) {
        return _.chain(value)
            .groupBy("VENUE_ID")
            .map(function(value1, venue_id) {
                return {
                    FlexCategoryName: key,
                    Cost: sum(_.pluck(value1, "Cost")),
                    Impressions: sum(_.pluck(value1, "Impressions")),
                    VENUE_ID: venue_id
                }
            })
            .value();
    })
    .value();
like image 71
thefourtheye Avatar answered Oct 18 '22 02:10

thefourtheye