Skip to main content

reduceRight()

Try it

reduceRight() 方法接受一个函数作为累加器(accumulator)和数组的每个值(从右到左)将其减少为单个值。

const array1 = [
[0, 1],
[2, 3],
[4, 5],
];

const result = array1.reduceRight((accumulator, currentValue) =>
accumulator.concat(currentValue)
);

console.log(result);
// expected output: Array [4, 5, 2, 3, 0, 1]

Example

展示 reduce 与 reduceRight 之间的区别

var a = ["1", "2", "3", "4", "5"];
var left = a.reduce(function (prev, cur) {
return prev + cur;
});
var right = a.reduceRight(function (prev, cur) {
return prev + cur;
});

console.log(left); // "12345"
console.log(right); // "54321"