简体   繁体   English

如何通过一些键求和数组中的对象值?

[英]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.

相关问题 如何按特定对象键对数组中的值求和? - How to sum values in array by specific object key? 按对象键减少对象数组并对值求和 - Reduce the array of object by object key and sum the values 如何对 object 值的数组求和并将它们分配给相关的键名 - How to sum the array of object values and assigned them to the relevant key name [javascript] [可迭代对象]如何总结可迭代对象(如数组)中特定键的值 - [javascript][iterable object] How to sum up the specific key's value in iterable object like array 如何将数组值作为对象键传递,并将该键与对象中的某些值相关联。 然后将其传递给列表项 - how to pass an array value as an object key, and associate that key with some values in the object. and then pass it into a list item 如何总结 javascript object 的值? - How to sum up values from a javascript object? 如何对 JavaScript 中具有相同值的数组求和? - How to sum up array with same values in JavaScript? 如何总结对象数组中的所有项目? - How to sum up all items in an object array? 如何在对象数组中使用相同的键对值求和? - How to sum values with the same key In an array of objects? 如何克隆一个只有一些键/值的对象? - How to clone an object but with only some key/values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM