[英]How to sum up object values in array by some key?
I cannot find solution to this problem. 我找不到解决此问题的方法。
I have array: 我有数组:
const taxItems = [{
taxCode: '430',
taxAmount: 2,
ItemTaxPercentage: 20,
taxCategory: '2'
},
{
taxCode: '430',
taxAmount: 4,
ItemTaxPercentage: 20,
taxCategory: '2'
},
{
taxCode: '431',
taxAmount: 5,
ItemTaxPercentage: 20,
taxCategory: '2'
},
{
taxCode: '430',
taxAmount: 6,
ItemTaxPercentage: 20,
taxCategory: '2'
}
]
And from this array I want to sum up all objects by taxCode.I want to have results like this: 从这个数组中,我想按taxCode汇总所有对象,我想要这样的结果:
var results = [{
taxCode: '430',
taxAmount: 12,
ItemTaxPercentage: 20,
taxCategory: '2'
},
{
taxCode: '431',
taxAmount: 5,
ItemTaxPercentage: 20,
taxCategory: '2'
}
]
I tried to use lodash: 我尝试使用lodash:
var results =
lodash(taxItems)
.groupBy('taxCode')
.map((objs, key) => ({
'taxCode': key,
'taxAmount': lodash.sumBy(objs, 'taxAmount') }))
.value();
But it only works with key value pair. 但它仅适用于键值对。 I will loose the original structure of the object.
我将松开对象的原始结构。
What would be the correct solution to my problem? 解决我的问题的正确方法是什么?
Spread the 1st object of each group into the result object, to get the recurring properties: 将每个组的第一个对象散布到结果对象中,以获得重复出现的属性:
const taxItems = [{"taxCode":"430","taxAmount":2,"ItemTaxPercentage":20,"taxCategory":"2"},{"taxCode":"430","taxAmount":4,"ItemTaxPercentage":20,"taxCategory":"2"},{"taxCode":"431","taxAmount":5,"ItemTaxPercentage":20,"taxCategory":"2"},{"taxCode":"430","taxAmount":6,"ItemTaxPercentage":20,"taxCategory":"2"}]; const results = _(taxItems) .groupBy('taxCode') .map((objs, taxCode) => ({ ..._.head(objs), taxCode, taxAmount: _.sumBy(objs, 'taxAmount') })) .value(); console.log(results);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
This will work. 这将起作用。 with a simple reduce.
简单的减少。 you don't need to use
lodash
for this. 您不需要为此使用
lodash
。
const results = taxItems.reduce(function(sum, taxItem) {
let index = sum.map(item => item.taxCode).indexOf(taxItem.taxCode);
if (index == -1) {
sum.push(taxItem);
} else {
sum[index].taxAmount = sum[index].taxAmount + taxItem.taxAmount;
}
return sum;
}, []);
//results
//[{"taxCode":"430","taxAmount":12,"ItemTaxPercentage":20,"taxCategory":"2"}{"taxCode":"431","taxAmount":5,"ItemTaxPercentage":20,"taxCategory":"2"}]
With simple reduce
you can do it this way. 使用简单的
reduce
可以做到这一点。
const taxItems = [{ taxCode: '430', taxAmount: 2, ItemTaxPercentage: 20, taxCategory: '2' }, { taxCode: '430', taxAmount: 4, ItemTaxPercentage: 20, taxCategory: '2' }, { taxCode: '431', taxAmount: 5, ItemTaxPercentage: 20, taxCategory: '2' }, { taxCode: '430', taxAmount: 6, ItemTaxPercentage: 20, taxCategory: '2' } ] let op = Object.values(taxItems.reduce((op,cur)=>{ if(op[cur['taxCode']]){ op[cur['taxCode']]['taxAmount']+= cur['taxAmount']; } else { op[cur['taxCode']] = cur } return op; },{})) console.log(op)
You could also solve this via _.map
, _.groupBy
and _.mergeWith
: 您也可以通过
_.map
, _.groupBy
和_.mergeWith
解决此_.mergeWith
:
const taxItems = [{ taxCode: '430', taxAmount: 2, ItemTaxPercentage: 20, taxCategory: '2' }, { taxCode: '430', taxAmount: 4, ItemTaxPercentage: 20, taxCategory: '2' }, { taxCode: '431', taxAmount: 5, ItemTaxPercentage: 20, taxCategory: '2' }, { taxCode: '430', taxAmount: 6, ItemTaxPercentage: 20, taxCategory: '2' } ] const result = _.map(_.groupBy(taxItems, 'taxCode'), x => _.mergeWith(...x, (o,s,k) => k == 'taxAmount' ? o+s : s)) console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.