reduceRight() method applies a function against an accumulator and each value of the array (from right-to-left) to reduce it to a single value.

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

另请参阅 Array.prototype.reduce() for left-to-right.

句法

arr.reduceRight(callback(accumulator, currentValue[, index[, array]])[, initialValue])
					

参数

callback
Function to execute on each value in the array, taking four arguments:
accumulator
The value previously returned in the last invocation of the callback, or initialValue , if supplied. (See below.)
currentValue

The current element being processed in the array.

index 可选

The index of the current element being processed in the array.

array 可选
The array reduceRight() was called upon.
initialValue 可选
Value to use as accumulator to the first call of the callback . If no initial value is supplied, the last element in the array will be used and skipped. Calling reduce or reduceRight on an empty array without an initial value creates a TypeError .

返回值

The value that results from the reduction.

描述

reduceRight executes the callback function once for each element present in the array, excluding holes in the array, receiving four arguments: the initial value (or value from the previous callback call), the value of the current element, the current index, and the array over which iteration is occurring.

The call to the reduceRight callback would look something like this:

array.reduceRight(function(accumulator, currentValue, index, array) {
  // ...
});
								

The first time the function is called, the accumulator and currentValue can be one of two values. If an initialValue was provided in the call to reduceRight , then accumulator will be equal to initialValue and currentValue will be equal to the last value in the array. If no initialValue was provided, then accumulator will be equal to the last value in the array and currentValue will be equal to the second-to-last value.

If the array is empty and no initialValue was provided, TypeError would be thrown. If the array has only one element (regardless of position) and no initialValue was provided, or if initialValue is provided but the array is empty, the solo value would be returned without calling callback .

Some example run-throughs of the function would look like this:

[0, 1, 2, 3, 4].reduceRight(function(accumulator, currentValue, index, array) {
  return accumulator + currentValue;
});
								

The callback would be invoked four times, with the arguments and return values in each call being as follows:

callback accumulator currentValue index array 返回值
first call 4 3 3 [0, 1, 2, 3, 4] 7
second call 7 2 2 [0, 1, 2, 3, 4] 9
third call 9 1 1 [0, 1, 2, 3, 4] 10
fourth call 10 0 0 [0, 1, 2, 3, 4] 10

The value returned by reduceRight would be that of the last callback invocation ( 10 ).

And if you were to provide an initialValue , the result would look like this:

[0, 1, 2, 3, 4].reduceRight(function(accumulator, currentValue, index, array) {
  return accumulator + currentValue;
}, 10);
								
callback accumulator currentValue index array 返回值
first call 10 4 4 [0, 1, 2, 3, 4] 14
second call 14 3 3 [0, 1, 2, 3, 4] 17
third call 17 2 2 [0, 1, 2, 3, 4] 19
fourth call 19 1 1 [0, 1, 2, 3, 4] 20
fifth call 20 0 0 [0, 1, 2, 3, 4] 20

The value returned by reduceRight this time would be, of course, 20 .

Polyfill

reduceRight was added to the ECMA-262 standard in the 5th edition; as such it may not be present in all implementations of the standard. You can work around this by inserting the following code at the beginning of your scripts, allowing use of reduceRight in implementations which do not natively support it.

// Production steps of ECMA-262, Edition 5, 15.4.4.22
// Reference: http://es5.github.io/#x15.4.4.22
if ('function' !== typeof Array.prototype.reduceRight) {
  Array.prototype.reduceRight = function(callback /*, initialValue*/) {
    'use strict';
    if (null === this || 'undefined' === typeof this) {
      throw new TypeError('Array.prototype.reduce called on null or undefined');
    }
    if ('function' !== typeof callback) {
      throw new TypeError(callback + ' is not a function');
    }
    var t = Object(this), len = t.length >>> 0, k = len - 1, value;
    if (arguments.length >= 2) {
      value = arguments[1];
    } else {
      while (k >= 0 && !(k in t)) {
        k--;
      }
      if (k < 0) {
        throw new TypeError('Reduce of empty array with no initial value');
      }
      value = t[k--];
    }
    for (; k >= 0; k--) {
      if (k in t) {
        value = callback(value, t[k], k, t);
      }
    }
    return value;
  };
}
								

范例

Sum up all values within an array

var sum = [0, 1, 2, 3].reduceRight(function(a, b) {
  return a + b;
});
// sum is 6
								

Flatten an array of arrays

var flattened = [[0, 1], [2, 3], [4, 5]].reduceRight(function(a, b) {
    return a.concat(b);
}, []);
// flattened is [4, 5, 2, 3, 0, 1]
								

Run a list of asynchronous functions with callbacks in series each passing their results to the next

const waterfall = (...functions) => (callback, ...args) =>
  functions.reduceRight(
    (composition, fn) => (...results) => fn(composition, ...results),
    callback
  )(...args);
const randInt = max => Math.floor(Math.random() * max)
const add5 = (callback, x) => {
  setTimeout(callback, randInt(1000), x + 5);
};
const mult3 = (callback, x) => {
  setTimeout(callback, randInt(1000), x * 3);
};
const sub2 = (callback, x) => {
  setTimeout(callback, randInt(1000), x - 2);
};
const split = (callback, x) => {
  setTimeout(callback, randInt(1000), x, x);
};
const add = (callback, x, y) => {
  setTimeout(callback, randInt(1000), x + y);
};
const div4 = (callback, x) => {
  setTimeout(callback, randInt(1000), x / 4);
};
const computation = waterfall(add5, mult3, sub2, split, add, div4);
computation(console.log, 5) // -> 14
// same as:
const computation2 = (input, callback) => {
  const f6 = x=> div4(callback, x);
  const f5 = (x, y) => add(f6, x, y);
  const f4 = x => split(f5, x);
  const f3 = x => sub2(f4, x);
  const f2 = x => mult3(f3, x);
  add5(f2, input);
}
								

​​​​​​Difference between reduce and 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"
								

Defining Composible Function

The concept of compose function is simple it combines n functions. It’s a flowing right-to-left, calling each function with the output of the last one.

/**
 * Function Composition is way in which result of one function can
 * be passed to another and so on.
 *
 * h(x) = f(g(x))
 *
 * Function execution happens right to left
 *
 * https://en.wikipedia.org/wiki/Function_composition
 */
const compose = (...args) => (value) => args.reduceRight((acc, fn) => fn(acc), value)
// Increment passed number
const inc = (n) => n + 1
// Doubles the passed value
const double = (n) => n * 2
// using composition function
console.log(compose(double, inc)(2)); // 6
// using composition function
console.log(compose(inc, double)(2)); // 5
								

规范

规范
ECMAScript (ECMA-262)
The definition of 'Array.prototype.reduceRight' in that specification.

浏览器兼容性

The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
reduceRight Chrome 3 Edge 12 Firefox 3 IE 9 Opera 10.5 Safari 5 WebView Android ≤37 Chrome Android 18 Firefox Android 4 Opera Android 14 Safari iOS 4 Samsung Internet Android 1.0 nodejs 0.1.100

图例

完整支持

完整支持

另请参阅

元数据

  • 最后修改:
  1. 标准内置对象
  2. Array
  3. 特性
    1. Array.prototype.length
    2. Array.prototype[@@unscopables]
  4. 方法
    1. Array.from()
    2. Array.isArray()
    3. Array.of()
    4. Array.prototype.concat()
    5. Array.prototype.copyWithin()
    6. Array.prototype.entries()
    7. Array.prototype.every()
    8. Array.prototype.fill()
    9. Array.prototype.filter()
    10. Array.prototype.find()
    11. Array.prototype.findIndex()
    12. Array.prototype.flat()
    13. Array.prototype.flatMap()
    14. Array.prototype.forEach()
    15. Array.prototype.includes()
    16. Array.prototype.indexOf()
    17. Array.prototype.join()
    18. Array.prototype.keys()
    19. Array.prototype.lastIndexOf()
    20. Array.prototype.map()
    21. Array.prototype.pop()
    22. Array.prototype.push()
    23. Array.prototype.reduce()
    24. Array.prototype.reduceRight()
    25. Array.prototype.reverse()
    26. Array.prototype.shift()
    27. Array.prototype.slice()
    28. Array.prototype.some()
    29. Array.prototype.sort()
    30. Array.prototype.splice()
    31. Array.prototype.toLocaleString()
    32. Array.prototype.toSource()
    33. Array.prototype.toString()
    34. Array.prototype.unshift()
    35. Array.prototype.values()
    36. Array.prototype[@@iterator]()
    37. get Array[@@species]
  5. 继承:
  6. Function
  7. 特性
    1. Function.arguments
    2. Function.caller
    3. Function.displayName
    4. Function.length
    5. Function.name
  8. 方法
    1. Function.prototype.apply()
    2. Function.prototype.bind()
    3. Function.prototype.call()
    4. Function.prototype.toSource()
    5. Function.prototype.toString()
  9. Object
  10. 特性
    1. Object.prototype.__proto__
    2. Object.prototype.constructor
  11. 方法
    1. Object.prototype.__defineGetter__()
    2. Object.prototype.__defineSetter__()
    3. Object.prototype.__lookupGetter__()
    4. Object.prototype.__lookupSetter__()
    5. Object.prototype.hasOwnProperty()
    6. Object.prototype.isPrototypeOf()
    7. Object.prototype.propertyIsEnumerable()
    8. Object.prototype.toLocaleString()
    9. Object.prototype.toSource()
    10. Object.prototype.toString()
    11. Object.prototype.valueOf()
    12. Object.setPrototypeOf()

Copyright  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1