Javascript 如何使用node js使用聚合函数
我有一个json,它的数组名为chargeamountunitLevel。我想通过按chargeAmountUnit分组来汇总chargeAmount。输入json:Javascript 如何使用node js使用聚合函数,javascript,node.js,group-by,aggregate-functions,Javascript,Node.js,Group By,Aggregate Functions,我有一个json,它的数组名为chargeamountunitLevel。我想通过按chargeAmountUnit分组来汇总chargeAmount。输入json: "chargeamountunitLevel": [ { "chargeAmount": 4, "chargeAmountUnit": "per hour", "currencyCode": "USD" },
"chargeamountunitLevel": [
{
"chargeAmount": 4,
"chargeAmountUnit": "per hour",
"currencyCode": "USD"
},
{
"chargeAmount": 50,
"chargeAmountUnit": "per hour",
"currencyCode": "USD"
},
{
"chargeAmount": 25,
"chargeAmountUnit": "per month",
"currencyCode": "USD"
},
{
"chargeAmount": 25,
"chargeAmountUnit": "per month",
"currencyCode": "USD"
}
]
结果可能如下:
"chargeamountunitLevel": [
{
"chargeAmount": 54,
"chargeAmountUnit": "per hour",
"currencyCode": "USD"
},
{
"chargeAmount": 50,
"chargeAmountUnit": "per month",
"currencyCode": "USD"
}
]
有什么建议吗?您可以使用下划线.js 代码如下:
var _ = require('underscore'); // use `npm install underscore`
var util = require('util'); // nodejs default modules
var data = {
"chargeamountunitLevel": [{
"chargeAmount": 4,
"chargeAmountUnit": "per hour",
"currencyCode": "USD"
}
, {
"chargeAmount": 50,
"chargeAmountUnit": "per hour",
"currencyCode": "USD"
}
, {
"chargeAmount": 25,
"chargeAmountUnit": "per month",
"currencyCode": "USD"
}
, {
"chargeAmount": 10,
"chargeAmountUnit": "per month",
"currencyCode": "USD"
}
, {
"chargeAmount": 1,
"chargeAmountUnit": "per month",
"currencyCode": "RMB"
}
, {
"chargeAmount": 25,
"chargeAmountUnit": "per month",
"currencyCode": "HKD"
}]
};
// This should give you an array of objects that
// are grouped by chargeAmountUnit.
var tmp = _.groupBy(data["chargeamountunitLevel"], function(d){
return d["chargeAmountUnit"];
});
// Show the temporary result :o)
console.log(tmp);
// Now group the result with currency code
var tmp2 = {};
_.each(tmp, function(t, unit){
tmp2[unit] = _.groupBy(t, function(d){
return d["currencyCode"];
});
});
// show the temp result again
console.log("tmp2: \n" + util.inspect(tmp2, false, null, true)); // util.inspect() is different in node v0.10.x
var finalResult = [];
_.each(tmp2, function(t, unit){
_.each(t, function(items, currency){
var total = 0;
_.each(items, function(item){
total += item["chargeAmount"]; // should also * currencyCode?
});
finalResult.push({
"chargeAmountUnit" : unit
, "chargeAmount" : total
, "currencyCode" : currency // Update it yourself :o)
});
});
});
console.log(finalResult);
你可以随时利用这个功能。在这里,由于我们提供了一个
initialValue
作为reduce
的第二个参数,因此第一次调用回调时,该值将作为结果
参数传递。然后,对于所有后续调用,result
将是上一次调用的返回值
这里,如果o
的chargeAmountUnit
已经作为键存在,我们基本上会检查initialValue
对象。如果没有,我们将创建一个新对象,该对象的属性值与对象o
相同,并使用chargeAmountUnit
作为键将其放入initialValue
对象中。但是,如果确实存在,我们将使用chargeAmountUnit
作为键检索先前创建的对象,并简单地汇总相关值
此外,您可能已经注意到,我们正在将创建的对象推送到一个数组中,这是因为最终您希望得到一个数组,而不是类似于:
{
'per month': {...},
'per hour': {...}
}
以下是如何做到这一点:
data.reduce(function (result, o) {
var unit = o.chargeAmountUnit;
if (!(unit in result)) {
result.arr.push(result[unit] = {
chargeAmountUnit: unit,
chargeAmount: o.chargeAmount,
currencyCode: o.currencyCode
});
} else {
result[unit].chargeAmount += o.chargeAmount;
}
return result;
}, { arr: [] }).arr;
编辑:要按多个字段分组,只需通过将分组按字段字符串值浓缩,按键创建分组
以下内容将分组到chargeAmountUnit
和currencyCode
data.reduce(function (result, o) {
//compute group by key
var key = o.chargeAmountUnit + o.currencyCode;
if (!(key in result)) {
result.arr.push(result[key] = {
chargeAmountUnit: o.chargeAmountUnit,
chargeAmount: o.chargeAmount,
currencyCode: o.currencyCode
});
} else {
result[key].chargeAmount += o.chargeAmount;
}
return result;
}, { arr: [] }).arr;
伟大的你的代码运行良好。如何按chargeAmountUnit和currencycode对chargeAmount进行分组。因为我需要在没有硬编码的情况下显示currencycode。(即使currencycode在数组中的所有值上都是统一的)@Prem,请注意,即使在我的答案中,它也只是在chargeAmountUnit
上分组,而不是currencycode
,但是修复它很容易。您只需计算一个新的键
。它将是chargeAmountUnit
和currencyCode
的串联,而不仅仅是chargeAmountUnit
。我同意@plalx所说的。您可以通过连接chargeAmountUnit
和currencyCode
(例如每月| HKD
)来创建新的“密钥”。但是我认为它没有使用两个groupBy()那么优雅,而且“麻烦”。您给出的方法工作得很好,但无法理解为什么我在中得到重复的值result@Prem,您能举个例子吗?我已决定获取副本(我在console.log(result)上获得了副本),但我无法从google获得的主要信息是,如果我想根据chargeAmountUnit和currencyCode进行分组,如何修改您的代码。在我的case@Prem,我添加了一个关于如何按多个字段分组的示例。