数组方法reduce
来源:互联网 发布:有淘宝账号怎么开网店 编辑:程序博客网 时间:2024/05/21 22:25
reduce()
方法接收一个函数作为累加器(accumulator),数组中的每个值(从左到右)开始合并,最终为一个值。
参数
callback
执行数组中每个值的函数,包含四个参数
previousValue
- 上一次调用回调返回的值,或者是提供的初始值(initialValue)
currentValue
- 数组中当前被处理的元素
index
- 当前元素在数组中的索引
array
- 调用
reduce
的数组
initialValue
- 作为第一次调用 callback 的第一个参数。
描述
reduce
为数组中的每一个元素依次执行回调函数,不包括数组中被删除或从未被赋值的元素,接受四个参数:初始值(或者上一次回调函数的返回值),当前元素值,当前索引,调用 reduce
的数组。
回调函数第一次执行时,previousValue
和 currentValue
可以是一个值,如果 initialValue 在调用 reduce 时被提供,那么第一个 previousValue 等于 initialValue ,并且currentValue 等于数组中的第一个值;如果initialValue 未被提供,那么previousValue 等于数组中的第一个值,currentValue等于数组中的第二个值。
如果数组为空并且没有提供initialValue, 会抛出TypeError
。如果数组仅有一个元素(无论位置如何)并且没有提供initialValue, 或者有提供initialValue但是数组为空,那么此唯一值将被返回并且callback不会被执行。
例如执行下面的代码
[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)。
如果把初始值作为第二个参数传入 reduce
,最终返回值变为20,结果如下:
[0,1,2,3,4].reduce(function(previousValue, currentValue, index, array){ return previousValue + currentValue;}, 10);
previousValue
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
例子
例子:将数组所有项相加
var total = [0, 1, 2, 3].reduce(function(a, b) { return a + b;});// total == 6
例子: 数组扁平化
var flattened = [[0, 1], [2, 3], [4, 5]].reduce(function(a, b) { return a.concat(b);});// flattened is [0, 1, 2, 3, 4, 5]
兼容旧环境(Polyfill)
Array.prototype.reduce
被添加到 ECMA-262 标准第 5 版;因此可能在某些实现环境中不被支持。可以将下面的代码插入到脚本开头来允许在那些未能原生支持 reduce
的实现环境中使用它。
if ('function' !== typeof Array.prototype.reduce) { Array.prototype.reduce = function(callback, opt_initialValue){ 'use strict'; if (null === this || 'undefined' === typeof this) { // At the moment all modern browsers, that support strict mode, have // native implementation of Array.prototype.reduce. For instance, IE8 // does not support strict mode, so this check is actually useless. throw new TypeError( 'Array.prototype.reduce called on null or undefined'); } if ('function' !== typeof callback) { throw new TypeError(callback + ' is not a function'); } var index, value, length = this.length >>> 0, isValueSet = false; if (1 < arguments.length) { value = opt_initialValue; isValueSet = true; } for (index = 0; length > index; ++index) { if (this.hasOwnProperty(index)) { if (isValueSet) { value = callback(value, this[index], index, this); } else { value = this[index]; isValueSet = true; } } } if (!isValueSet) { throw new TypeError('Reduce of empty array with no initial value'); } return value; };}
- 数组方法reduce
- javaSctipt数组的reduce方法
- JavaScript数组方法reduce解析
- javascript---数组array方法reduce()
- JavaScript数组map()、reduce()方法
- 数组reduce()和reduceRight()方法
- js求数组中的最大值(reduce方法)
- JavaScript数组forEach()、map()、reduce()方法
- 数组的reduce方法和redux
- 数组reduce方法的高级技巧
- javaScript--数组(Array)的reduce方法解析
- 详解JavaScript中数组的reduce方法
- 详解JavaScript中数组的reduce方法
- Javascript数组---map(),reduce()方法的区别
- JS数组reduce()和reduceRight()方法
- 数组 reduce 方法的高级技巧
- reduce方法
- reduce 方法
- 【UVa10601】Cubes-Burnside引理应用
- linux命令之文件命令集
- 二值图像的骨架提取
- java中集合和泛型(基础)
- Spark运行架构-简要版
- 数组方法reduce
- 无名管道pipe的使用
- Python 让PIP源使用国内镜像,提升下载速度和安装成功率
- Connected Cells in a Grid(dfs)
- 做一名合格的项目经理
- POJ3009 Curling 2.0【DFS】
- CentOS 7下安装UCSF Chimera
- Codeforces 235C : Cyclical Quest
- Web技术介绍和工作原理