es 5 数组reduce方法记忆
reduce() 方法接收一个函数作为累加器(accumulator),数组中的每个值(从左到右)开始合并,最终为一个值。
概念:对数组中的所有元素调用指定的回调函数。该回调函数的返回值为累积结果,并且此返回值在下一次调用该回调函数时作为参数提供。
语法:
array1.reduce(callbackfn[, initialValue])
参数:
|
参数 |
定义 |
|---|---|
|
array1 |
必需。一个数组对象。 |
|
callbackfn |
必需。一个接受最多四个参数的函数。对于数组中的每个元素,reduce 方法都会调用 callbackfn 函数一次。 |
|
initialValue |
可选。如果指定 initialValue,则它将用作初始值来启动累积。第一次调用 callbackfn 函数会将此值作为参数而非数组值提供。 |
通过最后一次调用回调函数获得的累积结果。
当满足下列任一条件时,将引发 TypeError 异常:
callbackfn 参数不是函数对象。
数组不包含元素,且未提供 initialValue。
如果提供了 initialValue,则 reduce 方法会对数组中的每个元素调用一次 callbackfn 函数(按升序索引顺序)。如果未提供 initialValue,则reduce 方法会对从第二个元素开始的每个元素调用 callbackfn 函数。
回调函数的返回值在下一次调用回调函数时作为 previousValue 参数提供。最后一次调用回调函数获得的返回值为 reduce 方法的返回值。
不为数组中缺少的元素调用该回调函数。
注意
reduceRight 方法 (Array) (JavaScript)按降序索引顺序处理元素。
回调函数的语法如下所示:
function callbackfn(previousValue, currentValue, currentIndex, array1)
可使用最多四个参数来声明回调函数。
下表列出了回调函数参数。
|
回调参数 |
定义 |
|---|---|
|
previousValue |
通过上一次调用回调函数获得的值。如果向 reduce 方法提供 initialValue,则在首次调用函数时,previousValue 为initialValue。 |
|
currentValue |
当前数组元素的值。 |
|
currentIndex |
当前数组元素的数字索引。 |
|
array1 |
包含该元素的数组对象。 |
在第一次调用回调函数时,作为参数提供的值取决于 reduce 方法是否具有 initialValue 参数。
如果向 reduce 方法提供 initialValue:
previousValue 参数为 initialValue。
currentValue 参数是数组中的第一个元素的值。
如果未提供 initialValue:
previousValue 参数是数组中的第一个元素的值。
currentValue 参数是数组中的第二个元素的值。
数组对象可由回调函数修改。
下表描述了在 reduce 方法启动后修改数组对象所获得的结果。
|
reduce 方法启动后的条件 |
元素是否传递给回调函数 |
|---|---|
|
在数组的原始长度之外添加元素。 |
否。 |
|
添加元素以填充数组中缺少的元素。 |
是,如果该索引尚未传递给回调函数。 |
|
元素被更改。 |
是,如果该元素尚未传递给回调函数。 |
|
从数组中删除元素。 |
否,除非该元素已传递给回调函数。 |
如果单纯看概念,一看就会头晕,但是看示例demo及输出,则对reduce的作用一目了然。
example:
[0,1,2,3,4].reduce(function(previousValue, currentValue, index, array){
return previousValue + currentValue;
});
//
[0,1,2,3,4].reduce(function(previousValue, currentValue, index, array){
return previousValue + currentValue;
},10);
//
[1].reduce(function(previousValue, currentValue, index, array){
return previousValue + currentValue;
},20);
//
//如果数组仅有一个元素(无论位置如何)并且没有提供initialValue, 或者有提供initialValue但是数组为空,那么此唯一值将被返回并且callback不会被执行。[至少有一个值为 “ 有 ”]
[1].reduce(function(previousValue, currentValue, index, array){
return previousValue + currentValue;
});
//
[].reduce(function(previousValue, currentValue, index, array){
return previousValue + currentValue;
},2);
//2
[null].reduce(function(previousValue, currentValue, index, array){
return previousValue + currentValue;
},2);
//
//如果出现值为Undefined,则输出NaN
[undefined].reduce(function(previousValue, currentValue, index, array){
return previousValue + currentValue;
},2);
//NaN
[1].reduce(function(previousValue, currentValue, index, array){
return previousValue + currentValue;
},undefined);
NaN
//如果数组为空并且没有提供initialValue, 会抛出TypeError [两个值都没有]
[].reduce(function(previousValue, currentValue, index, array){
return previousValue + currentValue;
});
//Uncaught TypeError: Reduce of empty array with no initial value(…)
{}.reduce(function(previousValue, currentValue, index, array){
return previousValue + currentValue;
},10);
{}.reduce(function(previousValue, currentValue, index, array){
return previousValue + currentValue;
});
//Uncaught SyntaxError: Unexpected token .
例子:将数组所有项相加
var total = [0, 1, 2, 3].reduce(function(a, b) {
return a + b;
});
console.log(total);
// total == 6
例子: 数组扁平化
var flattened = [[0, 1], [2, 3], [4, 5]].reduce(function(a, b) {
return a.concat(b);
});
console.log(flattened);
// flattened is [0, 1, 2, 3, 4, 5]
参考:http://www.cnblogs.com/leejersey/p/5466091.html
https://msdn.microsoft.com/library/ff679975(v=vs.94).aspx
es 5 数组reduce方法记忆的更多相关文章
- JS进阶篇--JS数组reduce()方法详解及高级技巧
基本概念 reduce() 方法接收一个函数作为累加器(accumulator),数组中的每个值(从左到右)开始缩减,最终为一个值. reduce 为数组中的每一个元素依次执行回调函数,不包括数组中被 ...
- 数组reduce方法以及高级技巧
基本概念: reduce()方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终为一个值. reduce为数组中的每一个元素依次执行回调函数.不包括数组中被删除或从未赋值的元素,接受两 ...
- js数组reduce()方法的使用和一些应用场景
reduce()的使用 reduce()方法为归并类方法,最常见的应用场景就是,计算数组中每一项的总和. reduce()方法会遍历数组的每一项,它接收两个参数: 第一个参数是:每次遍历都会调用的函数 ...
- 手写redux方法以及数组reduce方法
reduce能做什么? 1)求和 2)计算价格 3)合并数据 4)redux的compose方法 这篇文章主要内容是什么? 1)介绍reduce的主要作用 2)手写实现reduce方法 0)了解red ...
- ES6中的数组reduce()方法详解
reduce() 方法对数组中的每个元素执行一个由我们提供的reducer函数(升序执行),将其结果汇总为单个返回值. 1. 语法reduce说明 arr.reduce(callback(accumu ...
- JS数组reduce()方法详解及高级技巧
1.语法 arr.reduce(callback,[initialValue]) reduce 为数组中的每一个元素依次执行回调函数,不包括数组中被删除或从未被赋值的元素,接受四个参数:初始值(或者上 ...
- JS数组reduce()方法
1.语法 arr.reduce(callback,[initialValue]) reduce 为数组中的每一个元素依次执行回调函数,不包括数组中被删除或从未被赋值的元素,接受四个参数:初始值(或者上 ...
- 数组-reduce方法
转自: https://github.com/Advanced-Frontend/Daily-Interview-Question/issues/139 实现 convert 方法,把原始 list ...
- JS reduce()方法详解,使用reduce数组去重
壹 ❀ 引 稍微有了解JavaScript数组API的同学,对于reduce方法至少有过一面之缘,也许是for与forEach太强大,或者filter,find很实用,在实际开发中我至始至终没使用过 ...
随机推荐
- xorm的sql builder
最近在使用xorm,并使用了sql builder来构建sql查询没想到升级后原来可以使用的代码居然报错了. 0x00 代码 sql, args, _ := builder.Select(" ...
- win7+python3.6+word_cloud 安装出现Microsoft Visual C++ 14.0 is required
说明 环境: 已安装Anaconda3 (64-bit) 4.4.0(Python 3.6.1).其中,代码调试在Spyder 3.1.4中进行,安装包则直接打开Anaconda Prompt调用cm ...
- 解决Eclipse异常关闭后重启报 org.eclipse.swt.SWTException: Invalid thread access 的问题
. . . . . 很久没有写博客了,最近实在是太忙,一直想写点干货,但是一直没静下心来学习. 今天又在加班忙碌之中,结果谁知道越忙碌越出问题.先是 weblogic 没有正常启动,凭经验第一反应就是 ...
- GitHub限制上传单个大于100M的大文件
工作中遇到这个问题,一些美术资源..unitypackage文件大于100M,Push到GitHub时被拒绝.意思是Push到GitHub的每个文件的大小都要求小于100M. 搜了一下,很多解决办法只 ...
- [cpu]cpu unused pin应该怎样从硬件和软件上处理
Micro Community 1. This is a common question with lots of replies and lots of opinions. Preferred op ...
- Android——requestWindowFeature()的应用
Android开发中经常会在setContentView(R.layout.XXX); 前设置requestWindowFeature(XXXX). 他的意思是需要软件全屏显示.自定义标题(使用按钮等 ...
- Maven打包生成源码包和Javadoc包
https://blog.csdn.net/top_code/article/details/53586551 当我们开发了一个公共模块,将它deploy到Maven仓库时,最好同时提供源码包和Jav ...
- Spring Cloud Config 自动刷新所有节点 架构改造
详细参考:<Sprin Cloud 与 Docker 微服务架构实战>p162-9.9.4节 要做的改动是: 1.在spring cloud config server 服务端加入 spr ...
- WebRTC 源码分析(三):安卓视频硬编码
数据怎么送进编码器? 怎么从编码器取数据? 如何做流控? 在开始之前,我们先了解一下 MediaCodec 的基本知识. MediaCodec 基础 Developer 官网 上的描述已经很清楚了,下 ...
- sublime 之 vitage/emmet
ctrl+shift+p 打开Package Controlctrl+shift+t 恢复已关闭标签ctrl+/ 代码注释/取消注释 --------------------------------- ...