我如何使用lodash总结基于某些键的集合

时间:2017-08-27 04:25:01

标签: javascript lodash

我有以下Json数据,我想将记录与同一个customer_id合并,因此每个customer_id有一条记录

  [
   {
      "id":2,
      "customer_id":2,
      "amount":50,
      "total":100,
      "currency_code":"USD",
      "customer":{
         "id":2,
         "name":"ABC Company",
         "latest_comment":null
      }
   },
   {
      "id":3,
      "customer_id":3,
      "amount":90,
      "total":60,
      "currency_code":"USD",
      "customer":{
         "id":3,
         "name":"ABC Company 1"
      }
   },
   {
      "id":7,
      "customer_id":3,
      "amount":10,
      "total":40,
      "currency_code":"USD",
      "customer":{
         "id":3,
         "name":"ABC Company 1"
      }
   }
 ]

在此示例中有两个对象,其中customer_id = 3(第2和第3)

我想按customer_id汇总行,因此产生的集合看起来像

   [
       {
          "id":2,
          "customer_id":2,
          "amount":50,
          "total":100,
          "currency_code":"USD",
          "customer":{
             "id":2,
             "name":"ABC Company",
             "latest_comment":null
          }
       },
       {
          "id":null,             //just because there are multiple rows with same customer_id
          "customer_id":3,
          "amount":100,
          "total":100,
          "currency_code":"USD",
          "customer":{
             "id":3,
             "name":"ABC Company 1"
          }
       }
   ]

2 个答案:

答案 0 :(得分:1)

您可以使用以下代码获取总和:

var arr = [{
    "id": 2,
    "customer_id": 2,
    "amount": 50,
    "total": 100,
    "currency_code": "USD",
    "customer": {
      "id": 2,
      "name": "ABC Company",
      "latest_comment": null
    }
  },
  {
    "id": 3,
    "customer_id": 3,
    "amount": 90,
    "total": 60,
    "currency_code": "USD",
    "customer": {
      "id": 3,
      "name": "ABC Company 1"
    }
  },
  {
    "id": 7,
    "customer_id": 3,
    "amount": 10,
    "total": 40,
    "currency_code": "USD",
    "customer": {
      "id": 3,
      "name": "ABC Company 1"
    }
  }
]

var output =
  _(arr)
  .groupBy('customer_id')
  .map((objs, key) => ({

    'customer_id': key,
    'amount': _.sumBy(objs, 'amount'),
    'total': _.sumBy(objs, 'total')

  }))
  .value();

console.log(output);
<script src="https://cdn.jsdelivr.net/lodash/4.17.4/lodash.min.js"></script>

_.groupBy返回给定字段和

的聚合对象

_.sumBy返回迭代给定字段和数组的总和。

答案 1 :(得分:1)

使用_.groupBy()收集具有相同const data = [{"id":2,"customer_id":2,"amount":50,"total":100,"currency_code":"USD","customer":{"id":2,"name":"ABC Company","latest_comment":null}},{"id":3,"customer_id":3,"amount":90,"total":60,"currency_code":"USD","customer":{"id":3,"name":"ABC Company 1"}},{"id":7,"customer_id":3,"amount":10,"total":40,"currency_code":"USD","customer":{"id":3,"name":"ABC Company 1"}}]; var result = _(data) // group the object by customer_id .groupBy('customer_id') // merge each group .map((g) => _.mergeWith({}, ...g, (o, s, k) => { // if the key is amount or total add the numbers if((k === 'amount' || k === 'total') && _.isNumber(o)) { return o + s; } // if the key is id convert it to null if there's more than one if(k === 'id' && _.isNumber(o) && _.isNumber(s)) { return null; } })) .value(); console.log(result);的对象,然后使用_.mergeWith()将每个组合并为一个对象。

&#13;
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.2/lodash.min.js"></script>
&#13;
{{1}}
&#13;
&#13;
&#13;