Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

115
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda