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

306
Visualizações
array.reduce and null values

I am developing a javascript-based application, therein I have an array as below:

[-12.66, 268.2, 48.99, -1264.5, 20550, 91.2, 0, 0, 0, 0, 0, 0, 0, 0, 9235.5, 1500, 0, 0, 18.99, 0, 0, null, null, null, null, null, null, null, null, null, null]

now I want to do cumulative summation of below error until it returns null value, once it has started reading null value it should return null from there or should return as it is and should not do cumulative summation from there.

I have done as below using array.reduce:

let resultArray=[];
array1.reduce(function (acc, curr, index) {  return resuleArray[index] = acc + curr }, 0); 
// here array1 is source array.

However, this returns as below

[-12.66, 255.54, 304.53, -959.97, 19590.03, 19681.23, 19681.23, 19681.23, 19681.23, 19681.23, 19681.23, 19681.23, 19681.23, 19681.23, 28916.73, 30416.73, 30416.73, 30416.73, 30435.72, 30435.72, 30435.72, 30435.72, 30435.72, 30435.72, 30435.72, 30435.72, 30435.72, 30435.72, 30435.72, 30435.72, 30435.72];

What I would like to have is like below :

[-12.66, 255.54, 304.53, -959.97, 19590.03, 19681.23, 19681.23, 19681.23, 19681.23, 19681.23, 19681.23, 19681.23, 19681.23, 19681.23, 28916.73, 30416.73, 30416.73, 30416.73, 30435.72, 30435.72, null, null, null, null, null, null, null, null, null, null, null];

Please note that I will always have trailing values as null.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You need just to modify a little bit your code like this:

array1.reduce(function (acc, curr, index) {  
  return resultArray[index] = curr !== null ? acc + curr : null 
}, 0);
about 4 years ago · Juan Pablo Isaza Relatório

0

So, check if curr is null.

const array1 = [-12.66, 268.2, 48.99, -1264.5, 20550, 91.2, 0, 0, 0, 0, 0, 0, 0, 0, 9235.5, 1500, 0, 0, 18.99, 0, 0, null, null, null, null, null, null, null, null, null, null];

let resultArray = [];
array1.reduce(function(acc, curr, index) {
  const sum = acc + curr;
  if (curr === null) {
    resultArray[index] = null;
  } else {
    resultArray[index] = sum
  }
  return sum;
}, 0);

console.log(resultArray);

This way even if you put a number after some null values it will continue with the cumulative summation

about 4 years ago · Juan Pablo Isaza Relatório

0

You could take a closure over the sum and add unil the value is null.

const
    data = [-12.66, 268.2, 48.99, -1264.5, 20550, 91.2, 0, 0, 0, 0, 0, 0, 0, 0, 9235.5, 1500, 0, 0, 18.99, 0, 0, null, null, null, null, null, null, null, null, null, null],
    result = data.map((sum => v => sum === null || v === null
        ? sum = null
        : sum += v
    )(0));

console.log(...result);

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