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

Array.prototype.reduce() - 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.reduce()

Baseline Widely available

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

reduce() reducer (-), .

In this article

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

// 0 + 1 + 2 + 3 + 4
const initialValue = 0;
const sumWithInitial = array1.reduce(
  (accumulator, currentValue) => accumulator + currentValue,
  initialValue,
);

console.log(sumWithInitial);
// Expected output: 10

array.reduce(callback[, initialValue])

callback

, , :

accumulator

, , callback , initialValue, ( ).

currentValue

.

index

.

array

, reduce.

initialValue

. , callback.

reduce() callback , , , : ( callback), , , .

, accumulator currentValue . reduce() initialValue, accumulator initialValue, currentValue . initialValue , accumulator , currentValue .

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

, reduce() :

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

- , :

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

, reduce() - 10.

initialValue, :

js
[0, 1, 2, 3, 4].reduce(function (accumulator, currentValue, index, array) {
  return accumulator + currentValue;
}, 10);
accumulator currentValue index array
10 0 0 [0, 1, 2, 3, 4] 10
10 1 1 [0, 1, 2, 3, 4] 11
11 2 2 [0, 1, 2, 3, 4] 13
13 3 3 [0, 1, 2, 3, 4] 16
16 4 4 [0, 1, 2, 3, 4] 20

, reduce() , , 20.

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

, , initialValue, callback.

js
var initialValue = 0;
var sum = [{ x: 1 }, { x: 2 }, { x: 3 }].reduce(function (
  accumulator,
  currentValue,
) {
  return accumulator + currentValue.x;
}, initialValue);
// sum == 6

, :

js
var initialValue = 0;
var sum = [{ x: 1 }, { x: 2 }, { x: 3 }].reduce(
  (accumulator, currentValue) => accumulator + currentValue.x,
  initialValue,
);
// sum == 6

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

: , , initialValue

js
// friends -   ()
//   "books" -    
var friends = [
  { name: "Anna", books: ["Bible", "Harry Potter"], age: 21 },
  { name: "Bob", books: ["War and peace", "Romeo and Juliet"], age: 26 },
  { name: "Alice", books: ["The Lord of the Rings", "The Shining"], age: 18 },
];

// allbooks - ,       +
//     initialValue
var allbooks = friends.reduce(
  function (prev, curr) {
    return [...prev, ...curr.books];
  },
  ["Alphabet"],
);

// allbooks = ["Alphabet", "Bible", "Harry Potter", "War and peace",
// "Romeo and Juliet", "The Lord of the Rings", "The Shining"]

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

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

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


Web Proxy Viewer  |  New URL  |  Original Page