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

304
Views
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 answers
Answer question

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 Report

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 Report

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 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!