0

Just wondering if this is possible... I have a JSON array with 100 or so objects. Each object contains these key values. Job Number, Tax Amount, Line Total, and Line Total plus Tax. What I need to accomplish is creating a new JSON array with these key values. Job Number , Total Tax Amount, Sum of Tax Items, and Sum of non tax items. I only show an example of one job number, but there would be multiple Job Numbers.

[
   {
      "Job Number":200,
      "Tax Amount":5.00,
      "LineTotal":12,
      "Line Total plus tax":17.00
   },
   {
      "Job Number":200,
      "Tax Amount":2.00,
      "LineTotal":10,
      "Line Total plus tax":12.00
   },
   {
      "Job Number":200,
      "Tax Amount":0.00,
      "LineTotal":8,
      "Line Total plus tax":8
   }
]

I need to be able to look at all matching job numbers and sum the value of the other keys for that job number. One other twist... Sum of Tax Items only sums the values of items that have a tax amount and Sum of non tax items only sums the values of items with no tax amount. So the desired output would look like this...

[
   {
      "Job Number":200,
      "Total Tax Amount":7.00,
      "Sum of Tax Items":22.00,
      "Sum of non tax items":8
   }
]

I have a basic understanding on how to manipulate arrays, but this is something I haven't encountered before and not sure if this possible.

Thanks

  • how are the values `Sum of Tax Items: 22` and `Sum of non tax items: 8` calculated? And you probably want to use [`Array.reduce`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce) for such usecases – derpirscher Nov 19 '21 at 15:08
  • Possible duplicate of https://stackoverflow.com/questions/14446511/most-efficient-method-to-groupby-on-an-array-of-objects – Deniz Nov 19 '21 at 15:16
  • Does this answer your question? [Most efficient method to groupby on an array of objects](https://stackoverflow.com/questions/14446511/most-efficient-method-to-groupby-on-an-array-of-objects) – Deniz Nov 19 '21 at 15:16
  • Sum of Tax is calculated by adding the line total if that particular line has a tax value and vice versa for Non tax... – ItalianDadx2 Nov 19 '21 at 15:27

1 Answers1

0

You could use lodash.js and its groupBy() function.

Or you could roll your own. No more complicated than this:

function summarizeByJobNumber( list ) {
  const jobs = new Map();

  for (const item of list) {
    let value = map.get(item['Job Number']);

    if (!value) {

      value = {
        'Job Number': item['Job Number'],
        'Total Tax Amount': 0,
        'Sum of Tax Items': 0,
        'Sum of non tax items': 0,
      };
      map.set(value);

    }

    const isTaxable = item['Tax Amount'] !== 0;

    value[ 'Total Tax Amount'     ] += item['Tax Amount'];
    value[ 'Sum of Tax Items'     ] += isTaxable  ? item['Line Total'] : 0 ;
    value[ 'Sum of non tax items' ] += !isTaxable ? item['Line Total'] : 0 ;

  }

  return Array.from(jobs.values());
}
Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135