打字稿/破折号:如何获得具有多个通用属性的结果/对象

时间:2018-09-16 23:31:12

标签: javascript typescript filter group-by lodash

我正在寻找具有多个属性的groupBy函数的lodash / typerscript解决方案。 到目前为止,这是我尝试过的:

JSON:

[
    {
        "jobId": 1001,
        "id": 1,
        "timeSegmentId": 1,
        "balance": 100
    },
    {
        "jobId": 1001,
        "id": 2,
        "timeSegmentId": 1,
        "balance": 100
    },
    {
        "jobId": 1002,
        "id": 1,
        "timeSegmentId": 1,
        "balance": 100
    },
    {
        "jobId": 1002,
        "id": 1,
        "timeSegmentId": 1,
        "balance": 100
    },
    {
        "jobId": 1003,
        "id": 1,
        "timeSegmentId": 1,
        "balance": 100
    },
    {
        "jobId": 1003,
        "id": 1,
        "timeSegmentId": 2,
        "balance": 100
    },
    {
        "jobId": 1003,
        "id": 1,
        "timeSegmentId": 2,
        "balance": 100
    },
    {
        "jobId": 1003,
        "id": 1,
        "timeSegmentId": 2,
        "balance": 100
    },
    {
        "jobId": 1003,
        "id": 1,
        "timeSegmentId": 2,
        "balance": 100
    }
]

打字稿代码:

  // Group of same ids
  let mergeResults = _.groupBy(this.BSSDealresults, function (n) {
     return n.timeSegmentId;
  });
  const objKeys = Object.keys(mergeResults);
  const AggregateResults = [];
  objKeys.forEach(item => {
    if (mergeResults[item].length > 1) {

      let agg_balance = 0;

      mergeResults[item].forEach(element => {
        agg_balance += element.balance;
      });
      AggregateResults.push({
        id: mergeResults[item][0].id,
        jobId: mergeResults[item][0].jobId,
        balance: agg_balance
      });
    } else {
      AggregateResults.push(mergeResults[item][0]);
    }
  })

该解决方案仅根据我基于属性“ id”应用了破折号“ _groupBy”功能就可以按相同的“ id”对余额进行汇总。

鉴于此,我正在寻找基于多个属性的汇总结果,例如id,jobId和timeSegId。

lodash / typescript中有什么方法可以使用多个属性应用grouby吗?

预期输出应为:

[
    {
        "jobId": 1001,
        "id": 1,
        "timeSegmentId": 1,
        "balance": 100
    },
    {
        "jobId": 1001,
        "id": 2,
        "timeSegmentId": 1,
        "balance": 100
    },
    {
        "jobId": 1002,
        "id": 1,
        "timeSegmentId": 1,
        "balance": 200
    },
    {
        "jobId": 1003,
        "id": 1,
        "timeSegmentId": 1,
        "balance": 100
    },
    {
        "jobId": 1003,
        "id": 1,
        "timeSegmentId": 2,
        "balance": 100
    },
    {
        "jobId": 1003,
        "id": 1,
        "timeSegmentId": 2,
        "balance": 300
    }
]

1 个答案:

答案 0 :(得分:1)

您可以通过在3个ID中创建一个字符串来为_.groupBy()生成密钥。然后,您可以_.map()个组BSSDealresults对象,使用object spread(或Object.assign())克隆第一个元素,并使用_.sumBy()获得合并余额:

const BSSDealresults = [{"jobId":1001,"id":1,"timeSegmentId":1,"balance":100},{"jobId":1001,"id":2,"timeSegmentId":1,"balance":100},{"jobId":1002,"id":1,"timeSegmentId":1,"balance":100},{"jobId":1002,"id":1,"timeSegmentId":1,"balance":100},{"jobId":1003,"id":1,"timeSegmentId":1,"balance":100},{"jobId":1003,"id":1,"timeSegmentId":2,"balance":100}];

const mergeResults = _.groupBy(BSSDealresults, ({ id, jobId, timeSegmentId }) => `${id}-${jobId}-${timeSegmentId}`);

const AggregateResults = _.map(mergeResults, group => ({
  ...group[0],
  balance: _.sumBy(group, 'balance')
}));

console.log(AggregateResults);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>