1

我有一个 json,其中有一个名为 chargeamountunitLevel 的数组。我想通过按chargeAmountUnit分组来总结chargeAmount。输入json:

 "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"
        }

        ]

有什么建议么?

4

2 回答 2

3

您可以随时使用该reduce功能。在这里,由于我们将 aninitialValue作为第二个参数提供给reduce,因此第一次callback调用 时,该值将作为result参数传递。然后对于所有后续调用,result将是前一个调用的返回值。

在这里,如果of已经作为键存在,我们基本上会检查我们的initialValue对象。如果不是,我们创建一个与对象具有相同属性值的新对象,并使用as 键将其放入我们的对象中。但是,如果它确实存在,我们将使用作为键检索先前创建的对象,然后简单地总结相关值。chargeAmountUnitooinitialValuechargeAmountUnitchargeAmountUnit

此外,您可能已经注意到我们正在将创建的对象推送到数组中,这是因为最后您想要一个数组,而不是类似:

{
    '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;

key编辑:要按多个字段分组,您可以通过按字段字符串值连接分组来简单地创建一个组。

以下将在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;
于 2013-08-18T13:15:56.610 回答
0

你可以使用 underscore.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);
于 2013-08-18T13:06:04.247 回答