当前位置: 首页 > news >正文

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)按降序索引顺序处理元素。

回调函数语法

回调函数的语法如下所示:

1
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:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
[0,1,2,3,4].reduce( function (previousValue, currentValue, index, array){
   return  previousValue + currentValue;
});
  
//10
  
[0,1,2,3,4].reduce( function (previousValue, currentValue, index, array){
   return  previousValue + currentValue;
},10);
  
//20
   [1].reduce( function (previousValue, currentValue, index, array){
     return  previousValue + currentValue;
    },20);
  
  
   //21
  
  
//如果数组仅有一个元素(无论位置如何)并且没有提供initialValue, 或者有提供initialValue但是数组为空,那么此唯一值将被返回并且callback不会被执行。[至少有一个值为  “ 有 ”]
  
   [1].reduce( function (previousValue, currentValue, index, array){
     return  previousValue + currentValue;
    });
  
   //1
  
[].reduce( function (previousValue, currentValue, index, array){
   return  previousValue + currentValue;
},2);
//2
  
[ null ].reduce( function (previousValue, currentValue, index, array){
   return  previousValue + currentValue;
},2);
//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]




本文转自 蓓蕾心晴 51CTO博客,原文链接:http://blog.51cto.com/beileixinqing/1937213,如需转载请自行联系原作者

相关文章:

  • 用vlan划分实现全网互通,并隔离c1,c3和c2,c4
  • Nginx 目录配置详解
  • 详解最大似然估计(MLE)、最大后验概率估计(MAP),以及贝叶斯公式的理解...
  • linux之理解文件系统上的复制,移动,删除
  • Linux运维 第四阶段 (三) MySQL的SQL语句
  • C# GetSchema Get List of Table 获取数据库中所有的表名以及表中的纪录条数的方法
  • XML技术-Schema约束-Dom4j-Xpath详解
  • 从windows server的文件服务到分布式文件服务(二)
  • linux命令之uptime
  • LLDB调试工具简单使用
  • Linux必会原理之输入网址到看到页面内容原理
  • 通过RMAN备份duplicate异机克隆恢复数据库
  • 用C#设计一个四则运算器
  • j2se学习中的一些零碎知识点8之多线程
  • SCAC连接MicrosoftAzure
  • [nginx文档翻译系列] 控制nginx
  • 【跃迁之路】【735天】程序员高效学习方法论探索系列(实验阶段492-2019.2.25)...
  • 4月23日世界读书日 网络营销论坛推荐《正在爆发的营销革命》
  • electron原来这么简单----打包你的react、VUE桌面应用程序
  • Linux CTF 逆向入门
  • Redis学习笔记 - pipline(流水线、管道)
  • RxJS: 简单入门
  • 表单中readonly的input等标签,禁止光标进入(focus)的几种方式
  • 基于Javascript, Springboot的管理系统报表查询页面代码设计
  • 爬虫模拟登陆 SegmentFault
  • 漂亮刷新控件-iOS
  • 前端js -- this指向总结。
  • 浅谈Golang中select的用法
  • 巧用 TypeScript (一)
  • 如何选择开源的机器学习框架?
  • 少走弯路,给Java 1~5 年程序员的建议
  • 世界上最简单的无等待算法(getAndIncrement)
  • 微信小程序--------语音识别(前端自己也能玩)
  • 我的面试准备过程--容器(更新中)
  • 学习使用ExpressJS 4.0中的新Router
  • 一个完整Java Web项目背后的密码
  • d²y/dx²; 偏导数问题 请问f1 f2是什么意思
  • #162 (Div. 2)
  • #ubuntu# #git# repository git config --global --add safe.directory
  • (第27天)Oracle 数据泵转换分区表
  • (原創) 未来三学期想要修的课 (日記)
  • (源码版)2024美国大学生数学建模E题财产保险的可持续模型详解思路+具体代码季节性时序预测SARIMA天气预测建模
  • .Mobi域名介绍
  • .NET 8 中引入新的 IHostedLifecycleService 接口 实现定时任务
  • .NET CLR基本术语
  • .net core 调用c dll_用C++生成一个简单的DLL文件VS2008
  • .NET delegate 委托 、 Event 事件,接口回调
  • .NET MVC、 WebAPI、 WebService【ws】、NVVM、WCF、Remoting
  • .NET/C# 编译期能确定的字符串会在字符串暂存池中不会被 GC 垃圾回收掉
  • /usr/bin/perl:bad interpreter:No such file or directory 的解决办法
  • /usr/local/nginx/logs/nginx.pid failed (2: No such file or directory)
  • @zabbix数据库历史与趋势数据占用优化(mysql存储查询)
  • [ MSF使用实例 ] 利用永恒之蓝(MS17-010)漏洞导致windows靶机蓝屏并获取靶机权限
  • [Angular] 笔记 8:list/detail 页面以及@Input
  • [AutoSar]工程中的cpuload陷阱(三)测试