gpt4 book ai didi

javascript - typescript /lodash : how to get results/objects with multiple common properties

转载 作者:行者123 更新时间:2023-12-03 01:06:53 25 4
gpt4 key购买 nike

我正在寻找具有多个属性的 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
}
]

typescript 代码:

  // 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”分组的余额聚合,因为我应用了基于属性“id”的 lodash“_groupBy”函数。

而我正在寻找基于多个属性的聚合结果,例如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
}
]

最佳答案

您可以通过从 3 个 ID 中创建一个字符串来为 _.groupBy() 生成 key 。然后你就可以_.map() groups 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>

关于javascript - typescript /lodash : how to get results/objects with multiple common properties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52359242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com