Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

123
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda