From f5278dc186a1114025764e4e0d4c8ff7b60e2939 Mon Sep 17 00:00:00 2001 From: allo Date: Sun, 13 Mar 2022 09:47:38 +0800 Subject: convert to md and sync with english version --- .../reference/global_objects/array/reduce/index.md | 856 +++++++++------------ 1 file changed, 365 insertions(+), 491 deletions(-) diff --git a/files/zh-cn/web/javascript/reference/global_objects/array/reduce/index.md b/files/zh-cn/web/javascript/reference/global_objects/array/reduce/index.md index 348d717a0f..b0c839677c 100644 --- a/files/zh-cn/web/javascript/reference/global_objects/array/reduce/index.md +++ b/files/zh-cn/web/javascript/reference/global_objects/array/reduce/index.md @@ -11,385 +11,342 @@ tags: - Reference translation_of: Web/JavaScript/Reference/Global_Objects/Array/Reduce --- -

{{JSRef}}

- -

reduce() 方法对数组中的每个元素执行一个由您提供的reducer函数(升序执行),将其结果汇总为单个返回值。

- -
{{EmbedInteractiveExample("pages/js/array-reduce.html")}}
- -
-

reducer 函数接收4个参数:

- -
    -
  1. Accumulator (acc) (累计器)
  2. -
  3. Current Value (cur) (当前值)
  4. -
  5. Current Index (idx) (当前索引)
  6. -
  7. Source Array (src) (源数组)
  8. -
- -

您的 reducer 函数的返回值分配给累计器,该返回值在数组的每个迭代中被记住,并最后成为最终的单个结果值。

-
- -

语法

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

参数

- -
-
callback
-
执行数组中每个值 (如果没有提供 initialValue则第一个值除外)的函数,包含四个参数: -
-
accumulator
-
-

累计器累计回调的返回值; 它是上一次调用回调时返回的累积值,或initialValue(见于下方)。

-
-
currentValue
-
数组中正在处理的元素。
-
index {{optional_inline}}
-
数组中正在处理的当前元素的索引。 如果提供了initialValue,则起始索引号为0,否则从索引1起始。
-
array{{optional_inline}}
-
调用reduce()的数组
-
-
-
initialValue{{optional_inline}}
-
作为第一次调用 callback函数时的第一个参数的值。 如果没有提供初始值,则将使用数组中的第一个元素。 在没有初始值的空数组上调用 reduce 将报错。
-
- -

返回值

- -

函数累计处理的结果

- -

描述

- -

reduce为数组中的每一个元素依次执行callback函数,不包括数组中被删除或从未被赋值的元素,接受四个参数:

- - - -

回调函数第一次执行时,accumulatorcurrentValue的取值有两种情况:如果调用reduce()时提供了initialValueaccumulator取值为initialValuecurrentValue取数组中的第一个值;如果没有提供 initialValue,那么accumulator取数组中的第一个值,currentValue取数组中的第二个值。

- -
-

备注:如果没有提供initialValue,reduce 会从索引1的地方开始执行 callback 方法,跳过第一个索引。如果提供initialValue,从索引0开始。

-
- -

如果数组为空且没有提供initialValue,会抛出{{jsxref("TypeError")}} 。如果数组仅有一个元素(无论位置如何)并且没有提供initialValue, 或者有提供initialValue但是数组为空,那么此唯一值将被返回并且callback不会被执行。

- -

提供初始值通常更安全,正如下面的例子,如果没有提供initialValue,则可能有四种输出:

- -
var maxCallback = ( acc, cur ) => Math.max( acc.x, cur.x );
-var maxCallback2 = ( max, cur ) => Math.max( max, cur );
-
-// reduce() 没有初始值
-[ { x: 2 }, { x: 22 }, { x: 42 } ].reduce( maxCallback ); // NaN
-[ { x: 2 }, { x: 22 }            ].reduce( maxCallback ); // 22
-[ { x: 2 }                       ].reduce( maxCallback ); // { x: 2 }
-[                                ].reduce( maxCallback ); // TypeError
-
-// map/reduce; 这是更好的方案,即使传入空数组或更大数组也可正常执行
-[ { x: 22 }, { x: 42 } ].map( el => el.x )
-                        .reduce( maxCallback2, -Infinity );
-
- -

reduce() 如何运行

- -

假如运行下段reduce()代码:

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

callback 被调用四次,每次调用的参数和返回值如下表:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
callbackaccumulatorcurrentValuecurrentIndexarrayreturn value
first call011[0, 1, 2, 3, 4]1
second call122[0, 1, 2, 3, 4]3
third call333[0, 1, 2, 3, 4]6
fourth call644[0, 1, 2, 3, 4]10
- -

reduce返回的值将是最后一次回调返回值(10)。

- -

你还可以使用{{jsxref("Functions/Arrow_functions", "箭头函数","",1)}}来代替完整的函数。 下面的代码将产生与上面的代码相同的输出:

- -
[0, 1, 2, 3, 4].reduce((prev, curr) => prev + curr );
- -

如果你打算提供一个初始值作为reduce()方法的第二个参数,以下是运行过程及结果:

- -
[0, 1, 2, 3, 4].reduce((accumulator, currentValue, currentIndex, array) => {
-    return accumulator + currentValue
-}, 10)
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
callbackaccumulatorcurrentValuecurrentIndexarrayreturn value
first call1000[0, 1, 2, 3, 4]10
second call1011[0, 1, 2, 3, 4]11
third call1122[0, 1, 2, 3, 4]13
fourth call1333[0, 1, 2, 3, 4]16
fifth call1644[0, 1, 2, 3, 4]20
- -

这种情况下reduce()返回的值是20

- -

例子

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +{{JSRef}} +**`reduce()`** 方法对数组中的每个元素按序执行一个由您提供的 **reducer** 函数,每一次运行 **reducer** 会将先前元素的计算结果作为参数传入,最后将其结果汇总为单个返回值。 +第一次执行回调函数时,不存在“上一次的计算结果”。如果需要回调函数从数组索引为 0 的元素开始执行,则需要传递初始值。否则,数组索引为 0 的元素将被作为初始值 *initialValue*,迭代器将从第二个元素开始执行(索引为 1 而不是 0)。 +下面的例子能够帮助你理解 `reduce()` 的用处——计算数组所有元素的总和: +{{EmbedInteractiveExample("pages/js/array-reduce.html")}} +**reducer** 逐个遍历数组元素,每一步都将当前元素的值与上一步的计算结果相加(上一步的计算结果是当前元素之前所有元素的总和)——直到没有更多的元素被相加。 +## 语法 +```js +// Arrow function +reduce((previousValue, currentValue) => { /* ... */ } ) +reduce((previousValue, currentValue, currentIndex) => { /* ... */ } ) +reduce((previousValue, currentValue, currentIndex, array) => { /* ... */ } ) +reduce((previousValue, currentValue, currentIndex, array) => { /* ... */ }, initialValue) +// Callback function +reduce(callbackFn) +reduce(callbackFn, initialValue) +// Inline callback function +reduce(function(previousValue, currentValue) { /* ... */ }) +reduce(function(previousValue, currentValue, currentIndex) { /* ... */ }) +reduce(function(previousValue, currentValue, currentIndex, array) { /* ... */ }) +reduce(function(previousValue, currentValue, currentIndex, array) { /* ... */ }, initialValue) +``` +### 参数 +- `callbackFn` + - : 一个 “reducer” 函数,包含四个参数: + - `previousValue`:上一次调用 `callbackFn` 时的返回值。在第一次调用时,若指定了初始值 `initialValue`,其值则为 `initialValue`,否则为数组索引为 0 的元素 `array[0]`。 + - `currentValue`:数组中正在处理的元素。在第一次调用时,若指定了初始值 `initialValue`,其值则为数组索引为 0 的元素 `array[0]`,否则为 `array[1]`。 + - `currentIndex`:数组中正在处理的元素的索引。若指定了初始值 `initialValue`,则起始索引号为 0,否则从索引 1 起始。 + - `array`:用于遍历的数组。 +- `initialValue` {{optional_inline}} + - : 作为第一次调用 `callback` 函数时参数 *previousValue* 的值。若指定了初始值 `initialValue`,则 `currentValue` 则将使用数组第一个元素;否则 `previousValue` 将使用数组第一个元素,而 `currentValue` 将使用数组第二个元素。 +### 返回值 +使用 “reducer” 回调函数遍历整个数组后的结果。 +### 异常 +- {{jsxref("TypeError")}} + - : 数组为空且初始值 `initialValue` 未提供。 +## 描述 +ECMAScript 规范描述了 `reduce()` 的行为: +> *callbackfn* 应是一个接受四个参数的函数,`reduce` 对于数组中第一个元素之后的每一个元素,按升序各调用一次回调函数。 +> +> *callbackfn* 被调用时会传入四个参数: +> +> - *previousValue*(前一次调用 *callbackfn* 得到的返回值) +> - *currentValue*(数组中正在处理的元素) +> - *currentIndex*(数组中正在处理的元素的索引) +> - 被遍历的对象 +> +> 回调函数第一次执行时,*previousValue* 和 *currentValue* 的取值有两种情况: +> - 如果调用 `reduce()` 时提供了 *initialValue*,*previousValue* 取值则为 *initialValue*,*currentValue* 则取数组中的第一个值。 +> - 如果没有提供 *initialValue*,那么 *previousValue* 取数组中的第一个值,*currentValue* 取数组中的第二个值。 +> +> 如果数组为空且为指定初始值 *initialValue*,则会抛出 {{jsxref("TypeError")}}。 +> +> `reduce` 不会直接改变调用它的对象,但对象可被调用的 *callbackfn* 所改变。 +> +> 遍历的元素范围是在第一次调用 *callbackfn* 之前确定的。所以即使有元素在调用开始后被追加到数组中,这些元素也不会被 *callbackfn* 访问。如果数组现有的元素发生了变化,传递给 *callbackfn* 的值将会是元素被 `reduce` 访问时的值(即发生变化后的值);在调用 `reduce` 开始后,尚未被访问的元素若被删除,则其将不会被 `reduce` 访问。 +如果数组仅有一个元素(无论位置如何)并且没有提供初始值 *initialValue*,或者有提供 *initialValue* 但是数组为空,那么此唯一值将被返回且 `callbackfn` 不会被执行。 +提供初始值 *initialValue* 通常更安全,正如下面的例子,如果没有提供 *initialValue*,则 `reduce` 方法会因数组长度的不同(大于 1、等于 1、等于 0)而有不同的表现: +```js +const getMax = (a, b) => Math.max(a, b); +// callback is invoked for each element in the array starting at index 0 +[1, 100].reduce(getMax, 50); // 100 +[ 50].reduce(getMax, 10); // 50 +// callback is invoked once for element at index 1 +[1, 100].reduce(getMax); // 100 +// callback is not invoked +[ 50].reduce(getMax); // 50 +[ ].reduce(getMax, 1); // 1 +[ ].reduce(getMax); // TypeError +``` +### 无初始值时 reduce() 如何运行 +假如运行以下无初始值的 `reduce()` 代码: +```js +const array = [15, 16, 17, 18, 19]; +function reducer(previous, current, index, array) { + const returns = previous + current; + console.log(`previous: ${previous}, current: ${current}, index: ${index}, returns: ${returns}`); + return returns; +} +array.reduce(reducer); +``` +callback 被调用四次,每次调用的参数和返回值如下表: + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ callback iteration + + previousValue + + currentValue + + currentIndex + + array + return value
first call15161[15, 16, 17, 18, 19]31
second call31172[15, 16, 17, 18, 19]48
third call48183[15, 16, 17, 18, 19]66
fourth call66194[15, 16, 17, 18, 19]85
+由 `reduce()` 返回的值将是最后一次回调返回值(`85`)。 +### 有初始值时 reduce() 如何运行 +在这里,我们以相同的算法 reduce 同一个数组,但提供 `10` 作为初始值: +```js +[15, 16, 17, 18, 19].reduce( (previousValue, currentValue, currentIndex, array) => previousValue + currentValue, 10 ) +``` + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ callback iteration + + previousValue + + currentValue + + currentIndex + + array + return value
first call10150[15, 16, 17, 18, 19]25
second call25161[15, 16, 17, 18, 19]41
third call41172[15, 16, 17, 18, 19]58
fourth call58183[15, 16, 17, 18, 19]76
fifth call76194[15, 16, 17, 18, 19]95
+这种情况下 `reduce()` 返回的值是 `95`。 +## 示例 -

数组里所有值的和

+### 求数组所有值的和 -
var sum = [0, 1, 2, 3].reduce(function (accumulator, currentValue) {
-  return accumulator + currentValue;
-}, 0);
-// 和为 6
+```js +let sum = [0, 1, 2, 3].reduce(function (previousValue, currentValue) { + return previousValue + currentValue +}, 0) +// sum is 6 +``` -

你也可以写成箭头函数的形式:

+你也可以写成箭头函数的形式: -
var total = [ 0, 1, 2, 3 ].reduce(
-  ( acc, cur ) => acc + cur,
+```js
+let total = [ 0, 1, 2, 3 ].reduce(
+  ( previousValue, currentValue ) => previousValue + currentValue,
   0
-);
+) +``` -

累加对象数组里的值

+### 累加对象数组里的值 -

要累加对象数组中包含的值,必须提供初始值,以便各个item正确通过你的函数。

+要累加对象数组中包含的值,**必须**提供 *initialValue*,以便各个 item 正确通过你的函数。 -
var initialValue = 0;
-var sum = [{x: 1}, {x:2}, {x:3}].reduce(function (accumulator, currentValue) {
-    return accumulator + currentValue.x;
-},initialValue)
+```js
+let initialValue = 0
+let sum = [{x: 1}, {x: 2}, {x: 3}].reduce(function (previousValue, currentValue) {
+    return previousValue + currentValue.x
+}, initialValue)
 
-console.log(sum) // logs 6
+console.log(sum) // logs 6 +``` -

你也可以写成箭头函数的形式:

+你也可以写成箭头函数的形式: -
var initialValue = 0;
-var sum = [{x: 1}, {x:2}, {x:3}].reduce(
-    (accumulator, currentValue) => accumulator + currentValue.x
-    ,initialValue
-);
+```js
+let initialValue = 0
+let sum = [{x: 1}, {x: 2}, {x: 3}].reduce(
+    (previousValue, currentValue) => previousValue + currentValue.x
+    , initialValue
+)
 
 console.log(sum) // logs 6
-
+``` -

将二维数组转化为一维

+### 将二维数组转化为一维 -
var flattened = [[0, 1], [2, 3], [4, 5]].reduce(
-  function(a, b) {
-    return a.concat(b);
+```js
+let flattened = [[0, 1], [2, 3], [4, 5]].reduce(
+  function(previousValue, currentValue) {
+    return previousValue.concat(currentValue)
   },
   []
-);
+)
 // flattened is [0, 1, 2, 3, 4, 5]
-
+``` -

你也可以写成箭头函数的形式:

+你也可以写成箭头函数的形式: -
var flattened = [[0, 1], [2, 3], [4, 5]].reduce(
- ( acc, cur ) => acc.concat(cur),
- []
-);
-
-
+```js +let flattened = [[0, 1], [2, 3], [4, 5]].reduce( + ( previousValue, currentValue ) => previousValue.concat(currentValue), + [] +) +``` -

计算数组中每个元素出现的次数

+### 计算数组中每个元素出现的次数 -
var names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice'];
+```js
+let names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice']
 
-var countedNames = names.reduce(function (allNames, name) {
+let countedNames = names.reduce(function (allNames, name) {
   if (name in allNames) {
-    allNames[name]++;
+    allNames[name]++
   }
   else {
-    allNames[name] = 1;
+    allNames[name] = 1
   }
-  return allNames;
-}, {});
+  return allNames
+}, {})
 // countedNames is:
-// { 'Alice': 2, 'Bob': 1, 'Tiff': 1, 'Bruce': 1 }
+// { 'Alice': 2, 'Bob': 1, 'Tiff': 1, 'Bruce': 1 } +``` -

按属性对object分类

+### 按属性对 object 分类 -
var people = [
+```js
+let people = [
   { name: 'Alice', age: 21 },
   { name: 'Max', age: 20 },
   { name: 'Jane', age: 20 }
@@ -397,16 +354,16 @@ var countedNames = names.reduce(function (allNames, name) {
 
 function groupBy(objectArray, property) {
   return objectArray.reduce(function (acc, obj) {
-    var key = obj[property];
+    let key = obj[property]
     if (!acc[key]) {
-      acc[key] = [];
+      acc[key] = []
     }
-    acc[key].push(obj);
-    return acc;
-  }, {});
+    acc[key].push(obj)
+    return acc
+  }, {})
 }
 
-var groupedPeople = groupBy(people, 'age');
+let groupedPeople = groupBy(people, 'age')
 // groupedPeople is:
 // {
 //   20: [
@@ -415,13 +372,15 @@ var groupedPeople = groupBy(people, 'age');
 //   ],
 //   21: [{ name: 'Alice', age: 21 }]
 // }
-
+``` + +### 使用扩展运算符和 initialValue 绑定包含在对象数组中的数组 -

使用扩展运算符和initialValue绑定包含在对象数组中的数组

-
// friends - 对象数组
-// where object field "books" - list of favorite books
-var friends = [{
+```js
+// friends - an array of objects
+// where object field "books" is a list of favorite books
+let friends = [{
   name: 'Anna',
   books: ['Bible', 'Harry Potter'],
   age: 21
@@ -433,49 +392,59 @@ var friends = [{
   name: 'Alice',
   books: ['The Lord of the Rings', 'The Shining'],
   age: 18
-}];
+}]
 
 // allbooks - list which will contain all friends' books +
 // additional list contained in initialValue
-var allbooks = friends.reduce(function(prev, curr) {
-  return [...prev, ...curr.books];
-}, ['Alphabet']);
+let allbooks = friends.reduce(function(previousValue, currentValue) {
+  return [...previousValue, ...currentValue.books]
+}, ['Alphabet'])
 
 // allbooks = [
 //   'Alphabet', 'Bible', 'Harry Potter', 'War and peace',
 //   'Romeo and Juliet', 'The Lord of the Rings',
 //   'The Shining'
 // ]
-
+``` -

数组去重

+### 数组去重 -
-

备注: 如果你正在使用一个可以兼容{{jsxref("Set")}} 和 {{jsxref("Array.from()")}} 的环境, 你可以使用let orderedArray = Array.from(new Set(myArray)); 来获得一个相同元素被移除的数组。

-
+> **备注:** 如果你正在使用一个可以兼容{{jsxref("Set")}} 和 {{jsxref("Array.from()")}} 的环境,你可以使用`let arrayWithNoDuplicates = Array.from(new Set(myArray))` 来获得一个相同元素被移除的数组。 -
let myArray = ['a', 'b', 'a', 'b', 'c', 'e', 'e', 'c', 'd', 'd', 'd', 'd']
-let myOrderedArray = myArray.reduce(function (accumulator, currentValue) {
-  if (accumulator.indexOf(currentValue) === -1) {
-    accumulator.push(currentValue)
+```js
+let myArray = ['a', 'b', 'a', 'b', 'c', 'e', 'e', 'c', 'd', 'd', 'd', 'd']
+let myArrayWithNoDuplicates = myArray.reduce(function (previousValue, currentValue) {
+  if (previousValue.indexOf(currentValue) === -1) {
+    previousValue.push(currentValue)
   }
-  return accumulator
+  return previousValue
 }, [])
 
-console.log(myOrderedArray)
+console.log(myArrayWithNoDuplicates) +``` + +### 使用 .reduce() 替换 .filter().map() + +使用 {{jsxref("Array.filter()")}} 和 {{jsxref("Array.map()")}} 会遍历数组两次,而使用具有相同效果的 {{jsxref("Array.reduce()")}} 只需要遍历一次,这样做更加高效。(如果你喜欢 `for` 循环,你可用使用 {{jsxref("Array.forEach()")}} 以在一次遍历中实现过滤和映射数组) -
let arr = [1,2,1,2,3,5,4,5,3,4,4,4,4];
-let result = arr.sort().reduce((init, current) => {
-    if(init.length === 0 || init[init.length-1] !== current) {
-        init.push(current);
-    }
-    return init;
+```js
+const numbers = [-5, 6, 2, 0];
+
+const doubledPositiveNumbers = numbers.reduce((previousValue, currentValue) => {
+  if (currentValue > 0) {
+    const doubled = currentValue * 2;
+    previousValue.push(doubled);
+  }
+  return previousValue;
 }, []);
-console.log(result); //[1,2,3,4,5]
-

按顺序运行Promise

+console.log(doubledPositiveNumbers); // [12, 4] +``` + +### 按顺序运行 Promise -
/**
+```js
+/**
  * Runs promises from array of functions that can return promises
  * in chained manner
  *
@@ -484,190 +453,95 @@ console.log(result); //[1,2,3,4,5]
*/ function runPromiseInSequence(arr, input) { return arr.reduce( - (promiseChain, currentFunction) => promiseChain.then(currentFunction), + (promiseChain, currentFunction) => promiseChain.then(currentFunction), Promise.resolve(input) - ); + ) } // promise function 1 function p1(a) { - return new Promise((resolve, reject) => { - resolve(a * 5); - }); + return new Promise((resolve, reject) => { + resolve(a * 5) + }) } // promise function 2 function p2(a) { - return new Promise((resolve, reject) => { - resolve(a * 2); - }); + return new Promise((resolve, reject) => { + resolve(a * 2) + }) } // function 3 - will be wrapped in a resolved promise by .then() function f3(a) { - return a * 3; + return a * 3 } // promise function 4 function p4(a) { - return new Promise((resolve, reject) => { - resolve(a * 4); - }); + return new Promise((resolve, reject) => { + resolve(a * 4) + }) } -const promiseArr = [p1, p2, f3, p4]; +const promiseArr = [p1, p2, f3, p4] runPromiseInSequence(promiseArr, 10) - .then(console.log); // 1200 - + .then(console.log) // 1200 +``` -

功能型函数管道

+### 使用函数组合实现管道 -
// Building-blocks to use for composition
-const double = x => x + x;
-const triple = x => 3 * x;
-const quadruple = x => 4 * x;
+```js
+// Building-blocks to use for composition
+const double = x => x + x
+const triple = x => 3 * x
+const quadruple = x => 4 * x
 
 // Function composition enabling pipe functionality
-const pipe = (...functions) => input => functions.reduce(
-    (acc, fn) => fn(acc),
-    input
-);
+const pipe = (...functions) => initialValue => functions.reduce(
+    (acc, fn) => fn(acc),
+    initialValue
+)
 
 // Composed functions for multiplication of specific values
-const multiply6 = pipe(double, triple);
-const multiply9 = pipe(triple, triple);
-const multiply16 = pipe(quadruple, quadruple);
-const multiply24 = pipe(double, triple, quadruple);
+const multiply6 = pipe(double, triple)
+const multiply9 = pipe(triple, triple)
+const multiply16 = pipe(quadruple, quadruple)
+const multiply24 = pipe(double, triple, quadruple)
 
 // Usage
-multiply6(6); // 36
-multiply9(9); // 81
-multiply16(16); // 256
-multiply24(10); // 240
-
- -

使用 reduce实现map

- -
if (!Array.prototype.mapUsingReduce) {
-  Array.prototype.mapUsingReduce = function(callback, thisArg) {
-    return this.reduce(function(mappedArray, currentValue, index, array) {
-      mappedArray[index] = callback.call(thisArg, currentValue, index, array)
+multiply6(6)   // 36
+multiply9(9)   // 81
+multiply16(16) // 256
+multiply24(10) // 240
+```
+
+### 使用 reduce 实现 map
+
+```js
+if (!Array.prototype.mapUsingReduce) {
+  Array.prototype.mapUsingReduce = function(callback, initialValue) {
+    return this.reduce(function(mappedArray, currentValue, currentIndex, array) {
+      mappedArray[currentIndex] = callback.call(initialValue, currentValue, currentIndex, array)
       return mappedArray
     }, [])
   }
 }
 
 [1, 2, , 3].mapUsingReduce(
-  (currentValue, index, array) => currentValue + index + array.length
+  (currentValue, currentIndex, array) => currentValue + currentIndex + array.length
 ) // [5, 7, , 10]
-
- -

Polyfill

- -
// Production steps of ECMA-262, Edition 5, 15.4.4.21
-// Reference: http://es5.github.io/#x15.4.4.21
-// https://tc39.github.io/ecma262/#sec-array.prototype.reduce
-if (!Array.prototype.reduce) {
-  Object.defineProperty(Array.prototype, 'reduce', {
-    value: function(callback /*, initialValue*/) {
-      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');
-      }
-
-      // 1. Let O be ? ToObject(this value).
-      var o = Object(this);
-
-      // 2. Let len be ? ToLength(? Get(O, "length")).
-      var len = o.length >>> 0;
-
-      // Steps 3, 4, 5, 6, 7
-      var k = 0;
-      var value;
-
-      if (arguments.length >= 2) {
-        value = arguments[1];
-      } else {
-        while (k < len && !(k in o)) {
-          k++;
-        }
-
-        // 3. If len is 0 and initialValue is not present,
-        //    throw a TypeError exception.
-        if (k >= len) {
-          throw new TypeError( 'Reduce of empty array ' +
-            'with no initial value' );
-        }
-        value = o[k++];
-      }
-
-      // 8. Repeat, while k < len
-      while (k < len) {
-        // a. Let Pk be ! ToString(k).
-        // b. Let kPresent be ? HasProperty(O, Pk).
-        // c. If kPresent is true, then
-        //    i.  Let kValue be ? Get(O, Pk).
-        //    ii. Let accumulator be ? Call(
-        //          callbackfn, undefined,
-        //          « accumulator, kValue, k, O »).
-        if (k in o) {
-          value = callback(value, o[k], k, o);
-        }
-
-        // d. Increase k by 1.
-        k++;
-      }
-
-      // 9. Return accumulator.
-      return value;
-    }
-  });
-}
-
- -

如果您需要兼容不支持Object.defineProperty的JavaScript引擎,那么最好不要 polyfill Array.prototype方法,因为你无法使其成为不可枚举的。

- -

规范

- - - - - - - - - - - - - - - - - - - - - - - - -
SpecificationStatusComment
{{SpecName('ES5.1', '#sec-15.4.4.21', 'Array.prototype.reduce')}}{{Spec2('ES5.1')}}初始定语. 实施于 JavaScript 1.8.
{{SpecName('ES6', '#sec-array.prototype.reduce', 'Array.prototype.reduce')}}{{Spec2('ES6')}}
{{SpecName('ESDraft', '#sec-array.prototype.reduce', 'Array.prototype.reduce')}}{{Spec2('ESDraft')}}
+``` -

浏览器兼容性

+## 规范 -
+{{Specifications}} +## 浏览器兼容性 -

{{Compat("javascript.builtins.Array.reduce")}}

-
+{{Compat}} -

相关链接

+## 参见 - +- [Polyfill of `Array.prototype.reduce` in `core-js`](https://github.com/zloirock/core-js#ecmascript-array) +- {{jsxref("Array.prototype.reduceRight()")}} -- cgit v1.2.3-54-g00ecf