使用Underscore JS对JSON数组进行分组和聚合

时间:2022-04-09 19:41:32

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

我有一个json数组对象。我希望根据列“FlexCategoryName”对数据进行分组,并对矩阵列执行聚合,例如“成本”平均值,“点击”总和等。下面是JSON数组对象

Array[100]
0: Object
1: Object
2: Object
3: Object

0: Object

0:对象

Clicks: 1067
Cost: 4094.2
Date: "2/8/2014"
FlexCategoryName: "Cat1"
Impressions: 65943
Leads: 20

VENUE_ID:1

VENUE_ID:1

1: Object

1:对象

Clicks: 106
Cost: 409
Date: "2/8/2014"
FlexCategoryName: "Cat2"
Impressions: 65941
Leads: 21
VENUE_ID:1

2: Object

2:对象

Clicks: 106
Cost: 409
Date: "2/8/2014"
FlexCategoryName: "Cat2"
Impressions: 10
Leads: 21
VENUE_ID:2

3: Object

3:对象

Clicks: 106
Cost: 409
Date: "2/8/2014"
FlexCategoryName: "Cat3"
Impressions: 10
Leads: 21
VENUE_ID:2

4: Object

4:对象

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 .

如何使用Underscore JS对矩阵列执行分组和聚合。

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

1 个解决方案

#1


25  

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();

#1


25  

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();