mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-09 15:34:05 +08:00
531 lines
24 KiB
HTML
531 lines
24 KiB
HTML
<article id="wikiArticle">
|
||
<p></p><p></p>
|
||
<p><code><strong>reduce()</strong></code> 方法对数组中的每个元素执行一个由您提供的<strong>reducer</strong>函数(升序执行),将其结果汇总为单个返回值。</p>
|
||
<div><iframe class="interactive interactive-js" frameborder="0" height="250" src="https://interactive-examples.mdn.mozilla.net/pages/js/array-reduce.html" width="100%"></iframe></div>
|
||
<div class="hidden">
|
||
<p>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 <a class="external" href="https://github.com/mdn/interactive-examples" rel="noopener">https://github.com/mdn/interactive-examples</a> and send us a pull request. </p>
|
||
</div>
|
||
<div>
|
||
<p><strong>reducer</strong> 函数接收4个参数:</p>
|
||
<ol>
|
||
<li>Accumulator (acc) (累计器)</li>
|
||
<li>Current Value (cur) (当前值)</li>
|
||
<li>Current Index (idx) (当前索引)</li>
|
||
<li>Source Array (src) (源数组)</li>
|
||
</ol>
|
||
<p>您的 <strong>reducer</strong> 函数的返回值分配给累计器,该返回值在数组的每个迭代中被记住,并最后成为最终的单个结果值。</p>
|
||
</div>
|
||
<h2 id="语法">语法</h2>
|
||
<pre><var>arr</var>.reduce(<var>callback[, </var><var>initialValue]</var>)</code></pre>
|
||
<h3 id="参数">参数</h3>
|
||
<dl>
|
||
<dt><code>callback</code></dt>
|
||
<dd>执行数组中每个值的函数,包含四个参数:</dd>
|
||
<dd>
|
||
<dl>
|
||
</dl>
|
||
<strong><code>accumulator</code></strong>
|
||
<dl>
|
||
<dd>
|
||
<p>累计器累计回调的返回值; 它是上一次调用回调时返回的累积值,或<code>initialValue</code>(见于下方)。</p>
|
||
</dd>
|
||
<dt><code>currentValue</code></dt>
|
||
<dd>数组中正在处理的元素。</dd>
|
||
<dt><code>currentIndex</code><span class="inlineIndicator optional optionalInline">可选</span></dt>
|
||
<dd>数组中正在处理的当前元素的索引。 如果提供了<code>initialValue</code>,则起始索引号为0,否则为1。</dd>
|
||
<dt><code>array</code><span class="inlineIndicator optional optionalInline">可选</span></dt>
|
||
<dd>调用<code>reduce()</code>的数组</dd>
|
||
</dl>
|
||
</dd>
|
||
<dt><code>initialValue</code><span class="inlineIndicator optional optionalInline">可选</span></dt>
|
||
<dd>作为第一次调用 <code>callback函数时</code>的第一个参数的值。 如果没有提供初始值,则将使用数组中的第一个元素。 在没有初始值的空数组上调用 reduce 将报错。</dd>
|
||
</dl>
|
||
<h3 id="返回值">返回值</h3>
|
||
<p>函数累计处理的结果</p>
|
||
<h2 id="描述">描述</h2>
|
||
<p><code>reduce</code>为数组中的每一个元素依次执行<code>callback</code>函数,不包括数组中被删除或从未被赋值的元素,接受四个参数:</p>
|
||
<ul>
|
||
<li><code>accumulator 累计器</code></li>
|
||
<li><code>currentValue 当前值</code></li>
|
||
<li><code>currentIndex 当前索引</code></li>
|
||
<li><code>array 数组</code></li>
|
||
</ul>
|
||
<p>回调函数第一次执行时,<code>accumulator</code><span style="line-height: 1.5;"> 和</span><code>currentValue</code>的取值有两种情况:如果调用<code>reduce()</code>时提供了<code>initialValue</code>,<code>accumulator</code>取值为<code>initialValue</code>,<code>currentValue</code>取数组中的第一个值;如果没有提供 <code>initialValue</code>,那么<code>accumulator</code>取数组中的第一个值,<code>currentValue</code>取数组中的第二个值。</p>
|
||
<div class="note">
|
||
<p><strong>注意:</strong>如果没有提供<code>initialValue</code>,reduce 会从索引1的地方开始执行 callback 方法,跳过第一个索引。如果提供<code>initialValue</code>,从索引0开始。</p>
|
||
</div>
|
||
<p>如果数组为空且没有提供<code>initialValue</code>,会抛出<a href="Reference/Global_Objects/TypeError" title="TypeError(类型错误) 对象用来表示值的类型非预期类型时发生的错误。"><code>TypeError</code></a> 。如果数组仅有一个元素(无论位置如何)并且没有提供<code>initialValue</code>, 或者有提供<code>initialValue</code>但是数组为空,那么此唯一值将被返回并且<code>callback</code>不会被执行。</p>
|
||
<p>提供初始值通常更安全,正如下面的例子,如果没有提供<code>initialValue</code>,则可能有三种输出:</p>
|
||
<pre><code class="language-javascript">var maxCallback = ( acc, cur ) => Math.max( acc.x, cur.x );
|
||
var maxCallback2 = ( max, cur ) => Math.max( max, cur );
|
||
|
||
// reduce() 没有初始值
|
||
[ { x: 22 }, { x: 42 } ].reduce( maxCallback ); // 42
|
||
[ { x: 22 } ].reduce( maxCallback ); // { x: 22 }
|
||
[ ].reduce( maxCallback ); // TypeError
|
||
|
||
// map/reduce; 这是更好的方案,即使传入空数组或更大数组也可正常执行
|
||
[ { x: 22 }, { x: 42 } ].map( el => el.x )
|
||
.reduce( maxCallback2, -Infinity );
|
||
</code></pre>
|
||
<h3 id="reduce()_如何运行">reduce() 如何运行</h3>
|
||
<p>假如运行下段<code>reduce()</code>代码:</p>
|
||
<pre><code class="language-js">[0, 1, 2, 3, 4].reduce(function(accumulator, currentValue, currentIndex, array){
|
||
return accumulator + currentValue;
|
||
});
|
||
</code></pre>
|
||
<p>callback 被调用四次,每次调用的参数和返回值如下表:</p>
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th scope="col"><code>callback</code></th>
|
||
<th scope="col"><code>accumulator</code></th>
|
||
<th scope="col"><code>currentValue</code></th>
|
||
<th scope="col"><code>currentIndex</code></th>
|
||
<th scope="col"><code>array</code></th>
|
||
<th scope="col">return value</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<th scope="row">first call</th>
|
||
<td><code>0</code></td>
|
||
<td><code>1</code></td>
|
||
<td><code>1</code></td>
|
||
<td><code>[0, 1, 2, 3, 4]</code></td>
|
||
<td><code>1</code></td>
|
||
</tr>
|
||
<tr>
|
||
<th scope="row">second call</th>
|
||
<td><code>1</code></td>
|
||
<td><code>2</code></td>
|
||
<td><code>2</code></td>
|
||
<td><code>[0, 1, 2, 3, 4]</code></td>
|
||
<td><code>3</code></td>
|
||
</tr>
|
||
<tr>
|
||
<th scope="row">third call</th>
|
||
<td><code>3</code></td>
|
||
<td><code>3</code></td>
|
||
<td><code>3</code></td>
|
||
<td><code>[0, 1, 2, 3, 4]</code></td>
|
||
<td><code>6</code></td>
|
||
</tr>
|
||
<tr>
|
||
<th scope="row">fourth call</th>
|
||
<td><code>6</code></td>
|
||
<td><code>4</code></td>
|
||
<td><code>4</code></td>
|
||
<td><code>[0, 1, 2, 3, 4]</code></td>
|
||
<td><code>10</code></td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<table style="width: 100%;">
|
||
<tbody>
|
||
<tr>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<p>由<code>reduce</code>返回的值将是上次回调调用的值(10)。</p>
|
||
<p>你同样可以使用箭头函数的形式,下面的代码会输出跟前面一样的结果</p>
|
||
<p>您还可以提供<a href="Reference/Functions/Arrow_functions" title="箭头函数表达式的语法比函数表达式更简洁,并且没有自己的this,arguments,super或 new.target。这些函数表达式更适用于那些本来需要匿名函数的地方,并且它们不能用作构造函数。">Arrow Function</a> 来代替完整的函数。 下面的代码将产生与上面的代码中相同的输出:</p>
|
||
<pre><code class="language-javascript">[0, 1, 2, 3, 4].reduce((prev, curr) => prev + curr );</code></pre>
|
||
<p>如果你打算提供一个初始值作为<code>reduce()</code>方法的第二个参数,以下是运行过程及结果:</p>
|
||
<pre>[0, 1, 2, 3, 4].reduce((<code>accumulator</code>, currentValue, currentIndex, array) => { return <code>accumulator</code> + currentValue; }, 10 );</code></pre>
|
||
<table>
|
||
<thead>
|
||
<tr>
|
||
<th scope="col"><code>callback</code></th>
|
||
<th scope="col"><code>accumulator</code></th>
|
||
<th scope="col"><code>currentValue</code></th>
|
||
<th scope="col"><code>currentIndex</code></th>
|
||
<th scope="col"><code>array</code></th>
|
||
<th scope="col">return value</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr>
|
||
<th scope="row">first call</th>
|
||
<td><code>10</code></td>
|
||
<td><code>0</code></td>
|
||
<td><code>0</code></td>
|
||
<td><code>[0, 1, 2, 3, 4]</code></td>
|
||
<td><code>10</code></td>
|
||
</tr>
|
||
<tr>
|
||
<th scope="row">second call</th>
|
||
<td><code>10</code></td>
|
||
<td><code>1</code></td>
|
||
<td><code>1</code></td>
|
||
<td><code>[0, 1, 2, 3, 4]</code></td>
|
||
<td><code>11</code></td>
|
||
</tr>
|
||
<tr>
|
||
<th scope="row">third call</th>
|
||
<td><code>11</code></td>
|
||
<td><code>2</code></td>
|
||
<td><code>2</code></td>
|
||
<td><code>[0, 1, 2, 3, 4]</code></td>
|
||
<td><code>13</code></td>
|
||
</tr>
|
||
<tr>
|
||
<th scope="row">fourth call</th>
|
||
<td><code>13</code></td>
|
||
<td><code>3</code></td>
|
||
<td><code>3</code></td>
|
||
<td><code>[0, 1, 2, 3, 4]</code></td>
|
||
<td><code>16</code></td>
|
||
</tr>
|
||
<tr>
|
||
<th scope="row">fifth call</th>
|
||
<td><code>16</code></td>
|
||
<td><code>4</code></td>
|
||
<td><code>4</code></td>
|
||
<td><code>[0, 1, 2, 3, 4]</code></td>
|
||
<td><code>20</code></td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<p>这种情况下<code>reduce()</code>返回的值是<code>20</code>。</p>
|
||
<h2 id="例子">例子</h2>
|
||
<h3 id="数组里所有值的和">数组里所有值的和</h3>
|
||
<pre><code class="language-javascript">var sum = [0, 1, 2, 3].reduce(function (accumulator, currentValue) {
|
||
return accumulator + currentValue;
|
||
}, 0);
|
||
// 和为 6</code></pre>
|
||
<p>你也可以写成箭头函数的形式:</p>
|
||
<pre><code class="language-javascript">var total = [ 0, 1, 2, 3 ].reduce(
|
||
( acc, cur ) => acc + cur,
|
||
0
|
||
);</code></pre>
|
||
<h3 id="累加对象数组里的值">累加对象数组里的值</h3>
|
||
<p>要累加对象数组中包含的值,必须提供初始值,以便各个item正确通过你的函数。</p>
|
||
<pre><code class="language-javascript">var initialValue = 0;
|
||
var sum = [{x: 1}, {x:2}, {x:3}].reduce(function (accumulator, currentValue) {
|
||
return accumulator + currentValue.x;
|
||
},initialValue)
|
||
|
||
console.log(sum) // logs 6</code></pre>
|
||
<p>你也可以写成箭头函数的形式:</p>
|
||
<pre><code class="language-javascript">var initialValue = 0;
|
||
var sum = [{x: 1}, {x:2}, {x:3}].reduce(
|
||
(accumulator, currentValue) => accumulator + currentValue.x
|
||
,initialValue
|
||
);
|
||
|
||
console.log(sum) // logs 6
|
||
</code></pre>
|
||
<h3 id="将二维数组转化为一维">将二维数组转化为一维</h3>
|
||
<pre><code class="language-javascript">var flattened = [[0, 1], [2, 3], [4, 5]].reduce(
|
||
function(a, b) {
|
||
return a.concat(b);
|
||
},
|
||
[]
|
||
);
|
||
// flattened is [0, 1, 2, 3, 4, 5]
|
||
</code></pre>
|
||
<p>你也可以写成箭头函数的形式:</p>
|
||
<pre><code class="language-javascript">var flattened = [[0, 1], [2, 3], [4, 5]].reduce(
|
||
( acc, cur ) => acc.concat(cur),
|
||
[]
|
||
);
|
||
|
||
</code></pre>
|
||
<h3 id="计算数组中每个元素出现的次数">计算数组中每个元素出现的次数</h3>
|
||
<pre><code class="language-javascript">var names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice'];
|
||
|
||
var countedNames = names.reduce(function (allNames, name) {
|
||
if (name in allNames) {
|
||
allNames[name]++;
|
||
}
|
||
else {
|
||
allNames[name] = 1;
|
||
}
|
||
return allNames;
|
||
}, {});
|
||
// countedNames is:
|
||
// { 'Alice': 2, 'Bob': 1, 'Tiff': 1, 'Bruce': 1 }</code></pre>
|
||
<h3 id="按属性对object分类">按属性对object分类</h3>
|
||
<pre><code class="language-javascript">var people = [
|
||
{ name: 'Alice', age: 21 },
|
||
{ name: 'Max', age: 20 },
|
||
{ name: 'Jane', age: 20 }
|
||
];
|
||
|
||
function groupBy(objectArray, property) {
|
||
return objectArray.reduce(function (acc, obj) {
|
||
var key = obj[property];
|
||
if (!acc[key]) {
|
||
acc[key] = [];
|
||
}
|
||
acc[key].push(obj);
|
||
return acc;
|
||
}, {});
|
||
}
|
||
|
||
var groupedPeople = groupBy(people, 'age');
|
||
// groupedPeople is:
|
||
// {
|
||
// 20: [
|
||
// { name: 'Max', age: 20 },
|
||
// { name: 'Jane', age: 20 }
|
||
// ],
|
||
// 21: [{ name: 'Alice', age: 21 }]
|
||
// }
|
||
</code></pre>
|
||
<h3 id="使用扩展运算符和initialValue绑定包含在对象数组中的数组">使用扩展运算符和initialValue绑定包含在对象数组中的数组</h3>
|
||
<pre><code class="language-javascript">// friends - 对象数组
|
||
// where object field "books" - list of favorite 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 - list which will contain all friends' books +
|
||
// additional list contained in 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'
|
||
// ]
|
||
</code></pre>
|
||
<h3 id="数组去重">数组去重</h3>
|
||
<pre><code class="language-javascript">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;
|
||
}, []);
|
||
console.log(result); //[1,2,3,4,5]</code></pre>
|
||
<h3 id="按顺序运行Promise">按顺序运行Promise</h3>
|
||
<p> </p>
|
||
<pre><code class="language-javascript">/**
|
||
* Runs promises from array of functions that can return promises
|
||
* in chained manner
|
||
*
|
||
* @param {array} arr - promise arr
|
||
* @return {Object} promise object
|
||
*/
|
||
function runPromiseInSequence(arr, input) {
|
||
return arr.reduce(
|
||
(promiseChain, currentFunction) => promiseChain.then(currentFunction),
|
||
Promise.resolve(input)
|
||
);
|
||
}
|
||
|
||
// promise function 1
|
||
function p1(a) {
|
||
return new Promise((resolve, reject) => {
|
||
resolve(a * 5);
|
||
});
|
||
}
|
||
|
||
// promise function 2
|
||
function p2(a) {
|
||
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;
|
||
}
|
||
|
||
// promise function 4
|
||
function p4(a) {
|
||
return new Promise((resolve, reject) => {
|
||
resolve(a * 4);
|
||
});
|
||
}
|
||
|
||
const promiseArr = [p1, p2, f3, p4];
|
||
runPromiseInSequence(promiseArr, 10)
|
||
.then(console.log); // 1200
|
||
</code></pre>
|
||
<h3 id="功能型函数管道">功能型函数管道</h3>
|
||
<pre><code class="language-javascript">// 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
|
||
);
|
||
|
||
// 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);
|
||
|
||
// Usage
|
||
multiply6(6); // 36
|
||
multiply9(9); // 81
|
||
multiply16(16); // 256
|
||
multiply24(10); // 240
|
||
</code></pre>
|
||
<h2 id="Polyfill">Polyfill</h2>
|
||
<pre><code class="language-javascript">// 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;
|
||
}
|
||
});
|
||
}
|
||
</code></pre>
|
||
<p>如果您需要兼容不支持<code><a href="Reference/Global_Objects/Object/defineProperty">Object.defineProperty</a></code>的JavaScript引擎,那么最好不要 polyfill <code>Array.prototype</code>方法,因为你无法使其成为<strong>不可枚举</strong>的。</p>
|
||
<h2 id="规范">规范</h2>
|
||
<table class="standard-table">
|
||
<tbody>
|
||
<tr>
|
||
<th scope="col">Specification</th>
|
||
<th scope="col">Status</th>
|
||
<th scope="col">Comment</th>
|
||
</tr>
|
||
<tr>
|
||
<td><a class="external" href="https://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.21" hreflang="en" lang="en" rel="noopener">ECMAScript 5.1 (ECMA-262)<br/><small lang="zh-CN">Array.prototype.reduce</small></a></td>
|
||
<td><span class="spec-Standard">Standard</span></td>
|
||
<td>初始定语. 实施于 JavaScript 1.8.</td>
|
||
</tr>
|
||
<tr>
|
||
<td><a class="external" href="https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.reduce" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">Array.prototype.reduce</small></a></td>
|
||
<td><span class="spec-Standard">Standard</span></td>
|
||
<td> </td>
|
||
</tr>
|
||
<tr>
|
||
<td><a class="external" href="https://tc39.github.io/ecma262/#sec-array.prototype.reduce" hreflang="en" lang="en" rel="noopener">ECMAScript Latest Draft (ECMA-262)<br/><small lang="zh-CN">Array.prototype.reduce</small></a></td>
|
||
<td><span class="spec-Draft">Draft</span></td>
|
||
<td> </td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<h2 id="浏览器兼容性">浏览器兼容性</h2>
|
||
<div>
|
||
<div class="hidden">The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a class="external" href="https://github.com/mdn/browser-compat-data" rel="noopener">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</div>
|
||
<p></p><div class="bc-data"><a class="bc-github-link external" href="https://github.com/mdn/browser-compat-data" rel="noopener">Update compatibility data on GitHub</a><table class="bc-table bc-table-js"><thead><tr class="bc-platforms"><td></td><th class="bc-platform-desktop" colspan="6"><span>Desktop</span></th><th class="bc-platform-mobile" colspan="7"><span>Mobile</span></th><th class="bc-platform-server" colspan="1"><span>Server</span></th></tr><tr class="bc-browsers"><td></td><th class="bc-browser-chrome"><span class="bc-head-txt-label bc-head-icon-chrome">Chrome</span></th><th class="bc-browser-edge"><span class="bc-head-txt-label bc-head-icon-edge">Edge</span></th><th class="bc-browser-firefox"><span class="bc-head-txt-label bc-head-icon-firefox">Firefox</span></th><th class="bc-browser-ie"><span class="bc-head-txt-label bc-head-icon-ie">Internet Explorer</span></th><th class="bc-browser-opera"><span class="bc-head-txt-label bc-head-icon-opera">Opera</span></th><th class="bc-browser-safari"><span class="bc-head-txt-label bc-head-icon-safari">Safari</span></th><th class="bc-browser-webview_android"><span class="bc-head-txt-label bc-head-icon-webview_android">Android webview</span></th><th class="bc-browser-chrome_android"><span class="bc-head-txt-label bc-head-icon-chrome_android">Chrome for Android</span></th><th class="bc-browser-edge_mobile"><span class="bc-head-txt-label bc-head-icon-edge_mobile">Edge Mobile</span></th><th class="bc-browser-firefox_android"><span class="bc-head-txt-label bc-head-icon-firefox_android">Firefox for Android</span></th><th class="bc-browser-opera_android"><span class="bc-head-txt-label bc-head-icon-opera_android">Opera for Android</span></th><th class="bc-browser-safari_ios"><span class="bc-head-txt-label bc-head-icon-safari_ios">Safari on iOS</span></th><th class="bc-browser-samsunginternet_android"><span class="bc-head-txt-label bc-head-icon-samsunginternet_android">Samsung Internet</span></th><th class="bc-browser-nodejs"><span class="bc-head-txt-label bc-head-icon-nodejs">Node.js</span></th></tr></thead><tbody><tr><th scope="row"><a href="Reference/Global_Objects/Array/reduce"><code>reduce</code></a></th><td class="bc-supports-yes bc-browser-chrome"><span class="bc-browser-name">Chrome</span><abbr class="bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
</abbr>
|
||
Yes</td><td class="bc-supports-yes bc-browser-edge"><span class="bc-browser-name">Edge</span><abbr class="bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
</abbr>
|
||
12</td><td class="bc-supports-yes bc-browser-firefox"><span class="bc-browser-name">Firefox</span><abbr class="bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
</abbr>
|
||
3</td><td class="bc-supports-yes bc-browser-ie"><span class="bc-browser-name">IE</span><abbr class="bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
</abbr>
|
||
9</td><td class="bc-supports-yes bc-browser-opera"><span class="bc-browser-name">Opera</span><abbr class="bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
</abbr>
|
||
10.5</td><td class="bc-supports-yes bc-browser-safari"><span class="bc-browser-name">Safari</span><abbr class="bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
</abbr>
|
||
4</td><td class="bc-supports-yes bc-browser-webview_android"><span class="bc-browser-name">WebView Android</span><abbr class="bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
</abbr>
|
||
Yes</td><td class="bc-supports-yes bc-browser-chrome_android"><span class="bc-browser-name">Chrome Android</span><abbr class="bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
</abbr>
|
||
Yes</td><td class="bc-supports-yes bc-browser-edge_mobile"><span class="bc-browser-name">Edge Mobile</span><abbr class="bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
</abbr>
|
||
Yes</td><td class="bc-supports-yes bc-browser-firefox_android"><span class="bc-browser-name">Firefox Android</span><abbr class="bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
</abbr>
|
||
4</td><td class="bc-supports-yes bc-browser-opera_android"><span class="bc-browser-name">Opera Android</span><abbr class="bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
</abbr>
|
||
Yes</td><td class="bc-supports-yes bc-browser-safari_ios"><span class="bc-browser-name">Safari iOS</span><abbr class="bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
</abbr>
|
||
Yes</td><td class="bc-supports-yes bc-browser-samsunginternet_android"><span class="bc-browser-name">Samsung Internet Android</span><abbr class="bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
</abbr>
|
||
Yes</td><td class="bc-supports-yes bc-browser-nodejs"><span class="bc-browser-name">nodejs</span><abbr class="bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
</abbr>
|
||
Yes</td></tr></tbody></table><section class="bc-legend" id="sect1"><h3 class="offscreen" id="Legend">Legend</h3><dl><dt><span class="bc-supports-yes bc-supports">
|
||
<abbr class="bc-level bc-level-yes only-icon" title="Full support">
|
||
<span>Full support</span>
|
||
|
||
</abbr></span></dt><dd>Full support</dd></dl></section></div><p></p>
|
||
</div>
|
||
<h2 id="相关链接">相关链接</h2>
|
||
<ul>
|
||
<li><a href="Reference/Global_Objects/Array/reduceRight" title="reduceRight() 方法接受一个函数作为累加器(accumulator)和数组的每个值(从右到左)将其减少为单个值。"><code>Array.prototype.reduceRight()</code></a></li>
|
||
</ul>
|
||
</article> |