在特定条件下计算数组中的值

Calculate value in array under certain condition

我有如下数组:

const array = [1, 3, 5, 7, 9, 11, 8, 10, 13, 15];

我想对数组中小于 10 的值求和,求和变成大于 10,如下所示:

const newArray = [16, 20, 18, 13, 15]; // 16 at index 0 is from (1+3+5+7), 20 at index 1 is from (9+11), 18 at index 2 is from (8+10)

这是我尝试过的,但我从这里卡住了:

const minTen = array.reduce((accumulator, currentValue) => {
    
    if (currentValue < 10) {

        // 1. Sum value in array to become >= 10
        const accumValue = currentValue += // help here please or any othr alternative

        // 2. Push new value to the array
        accumulator.push(accumValue);
    }
    return accumulator;
}, []);

console.log(minTen); // [16, 20, 18, 13, 15]

如果你真的想用Array.reduce :

const array = [1, 3, 5, 7, 9, 11, 13, 15];
let sum = 0;
const minTen = array.reduce((acc, currentValue) => {
        if (currentValue < 10) {
            sum += currentValue
        } else {
            acc = [...acc, currentValue]
        }
        return acc
}, []);

console.log([...minTen, sum]);

但我不确定这是最好的方法。

步骤:

  1. 遍历给定的数组并给它一个条件 - 如果数字小于 10 则求和,否则将其推入结果数组。
  2. 将最后的总和插入结果数组的第一位。
  3. 完成

const array = [1, 3, 5, 7, 9, 11, 13, 15]

let sum = 0, resultArr = []
array.forEach(i => i<10 ? sum += i : resultArr.push(i))
resultArr = [sum, ...resultArr]
console.log(resultArr)

array.reduce 是我不常用的现代函数之一。老实说,我不知道它是如何工作的。如果 DBS 是正确的,那么您也不对。这样您就可以轻松完成。

for (var i=0, n=0, newArray = [];i<array.length;i++) {
    if (array[i] >= 10) {
        newArray.push(array[i]);
    } else {
        n += array[i];
    }
}
newArray.unshift(n);

这里使用过滤函数,只对大于 10 的进行相加,对小于 10 的进行求和。最后,通过调用 Array.prototype.splice:

将总和插入到数组的索引 0 处

 const array = [1, 3, 5, 7, 9, 11, 13, 15];
 
 let underTen = 0;
 const minTen = array.filter(val => {
     if (val < 10) {
        underTen += val;
     } else {
        return val;
     }
});

// add sum at pos 1
minTen.splice(0, 0, underTen);
console.log(minTen);

我不鼓励在同一个函数中执行 2 个不同的操作,过滤和求和。干净的解决方案总是一次只做一件事。这里的解决方案是在自动单元测试的上下文中提出的:

test('62893326', () => {
  const input = [1, 3, 5, 7, 9, 11, 13, 15]
  const expected = [25, 11, 13, 15]
  const lower = input.filter(i => i < 10)
  const higher = input.filter(i => i >= 10)
  const sum = lower.reduce((accumulator, currentValue) => accumulator + currentValue)
  const result = [sum, ...higher]
  expect(result).toEqual(expected)
})

只检查结果集的最后一个值。

const
    array = [1, 3, 5, 7, 9, 11, 8, 10, 13, 15],
    minTen = array.reduce((accu, value) => {
        if (accu[accu.length - 1] < 10) accu[accu.length - 1] += value;
        else accu.push(value);
        return accu;
    }, []);

console.log(minTen); // [25, 11, 18, 13, 15]

经过一段时间的试验,我发现这段主要使用原始操作而不是库函数的代码对初始数组中的数字求和,直到形成大于 10 的值,并在总和为 11 或更大:

const array = [1, 3, 5, 7, 9, 11, 8, 10, 13, 15];
 let sum = 0;
 let arrayLength = array.length;
for(var i = 0; i < arrayLength; i++) {
    if (array[i] <= 10) {
        sum += array[i];
        let n = i+1;
        if (n < arrayLength) {
            do {
                sum += array[n];
                n++;
            }
            // keep adding numbers from the array until a sum of 11 or greater
        while((n) < arrayLength && array[n] <= 10 && sum < 11);
        }
        // replace the numbers in the array that were summed together
        // with the sum of those numbers.
        array.splice(i, n - i, sum);
        sum = 0; // reset for next iteration
    }
    // the for loop advances to the next position in the array automatically
}

console.log(array); // outputs [16, 20, 18, 13, 15]