array.reduce 和空值

array.reduce and null values

我正在开发一个基于javascript的应用程序,其中我有一个数组如下:

[-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]

现在我想对以下错误进行累积求和,直到它 return 为空值,一旦它开始读取空值,它应该 return 从那里为空或者应该 return因为它是,不应该从那里做累积总和。

我使用 array.reduce 做了如下操作:

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

但是,这个return如下

[-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];

我想要的是如下所示:

[-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];

请注意,我将始终将尾随值设为 null。

您只需像这样稍微修改一下代码:

array1.reduce(function (acc, curr, index) {  
  return resultArray[index] = curr !== null ? acc + curr : null 
}, 0);

因此,检查 curr 是否为 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);

这样即使您在某些 null 值之后放置一个数字,它也会继续累加

您可以对总和进行闭包并相加直到值为 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);

一旦 null 被解释,就在索引的其余部分插入 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 = [];
let isNullInterpreted = false;
const output = array1.reduce(function (acc, curr, index) {
  isNullInterpreted = isNullInterpreted || curr == null;
  return resultArray[index] = isNullInterpreted ? null : acc + curr;
}, 0);
console.log(resultArray);
console.log(output);

解释 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 = [];
let isNullInterpreted = false;
const output = array1.reduce(function (acc, curr, index) {
  isNullInterpreted = isNullInterpreted || curr == null;
  if (!isNullInterpreted) {
    resultArray[index] = acc + curr;
  }
  return resultArray[resultArray.length - 1]
}, 0);
console.log(resultArray);
console.log(output);