Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

114
Views
How to merge / combine elements of a JSON array into a new JSON array in NodeJS

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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

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());
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!