[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Array/reduceRight [Back]  [Original]

Array.prototype.reduceRight() - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Array.prototype.reduceRight()

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 ..

reduceRight() (-), .

In this article

arr.reduceRight(callback[, initialValue])

callback

, , :

previousValue

, callback, initialValue, ( ).

currentValue

.

index

.

array

, reduceRight.

initialValue

. , callback.

reduceRight() callback , , , : ( callback), , , .

- callback :

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

, previousValue currentValue . reduceRight() initialValue, previousValue initialValue, currentValue . initialValue , previousValue , currentValue .

initialValue , TypeError. ( ) initialValue , initialValue , , , callback.

:

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

- , :

previousValue currentValue index array
4 3 3 [0, 1, 2, 3, 4] 7
7 2 2 [0, 1, 2, 3, 4] 9
9 1 1 [0, 1, 2, 3, 4] 10
10 0 0 [0, 1, 2, 3, 4] 10

, reduceRight() - 10.

initialValue, :

js
[0, 1, 2, 3, 4].reduceRight(function (
  previousValue,
  currentValue,
  index,
  array,
) {
  return previousValue + currentValue;
}, 10);
previousValue currentValue index array
10 4 4 [0, 1, 2, 3, 4] 14
14 3 3 [0, 1, 2, 3, 4] 17
17 2 2 [0, 1, 2, 3, 4] 19
19 1 1 [0, 1, 2, 3, 4] 20
20 0 0 [0, 1, 2, 3, 4] 20

, reduceRight() , , 20.

:

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

:

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

Array.prototype.reduceRight() ECMA-262 5- ; . , , reduceRight() , .

js
//   ECMA-262, 5- , 15.4.4.22
//  (en): http://es5.github.io/#x15.4.4.22
//  (ru): http://es5.javascript.ru/x15.4.html#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;
  };
}

Specification
ECMAScript 2027 LanguageSpecification
# sec-array.prototype.reduceright


Web Proxy Viewer  |  New URL  |  Original Page