0

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:

var results = [{
    taxCode: '430',
    taxAmount: 12,
    ItemTaxPercentage: 20,
    taxCategory: '2'
  },
  {
    taxCode: '431',
    taxAmount: 5,
    ItemTaxPercentage: 20,
    taxCategory: '2'
  }
]

I tried to use 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?

3
  • 1
    Have a look at reduce Commented Dec 16, 2018 at 16:57
  • @trincot - his problem is not the summing up, but the passing of other props into the result. Commented Dec 16, 2018 at 17:05
  • 1
    @OriDrori, OK, reopened ;-) Commented Dec 16, 2018 at 17:21

4 Answers 4

1

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>

Sign up to request clarification or add additional context in comments.

Comments

0

With simple reduce you can do it this way.

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)

Comments

0

This will work. with a simple reduce. you don't need to use lodash for this.

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"}]

Comments

0

You could also solve this via _.map, _.groupBy and _.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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.