当前位置:网站首页>js:Array.reduce累加计算、合并数组
js:Array.reduce累加计算、合并数组
2022-06-29 06:41:00 【彭世瑜】
语法
array.reduce(function(prevValue, currentValue, currentIndex, arr), initialValue)
| 参数 | 描述 |
|---|---|
| prevValue | 必需。上一个值, 或者计算结束后的返回值。 |
| currentValue | 必需。当前元素 |
| currentIndex | 可选。当前元素的索引 |
| arr | 可选。当前元素所属的数组对象。 |
| initialValue | 可选。传递给函数的初始值 |
示例:累加计算
let list = [1, 2, 3, 5];
let result = list.reduce((prevValue, currentValue)=>{
return prevValue + currentValue;
}, 0);
console.log(result);
// 11
示例:合并数组
let list = [[1, 2, 3], [], [8, 9]];
let result = list.reduce((prevArr, currentArr)=>{
return prevArr.concat(currentArr);
}, []);
console.log(result);
// [ 1, 2, 3, 8, 9 ]
边栏推荐
- 环境准备 - 工程管理
- [量化投资系统]Django从数据库中实现筛选及分页
- 1031 Hello World for U
- About the problem that the kingbasees temporary file is too large
- Cartographer中的线程池操作
- Electric check code configuration
- postman预处理/前置条件Pre-request
- How to permanently set Mysql to utf8 encoding?
- 4年工作经验,多线程间的5种通信方式都说不出来,你敢信?
- 关于开发web场景下如何解决手机访问web跨域问题
猜你喜欢

Problem solving -- > online OJ (13)

Schnuka: 3D machine vision inspection system 3D vision inspection application industry
![[industrial control old horse] detailed design of PLC six way responder system](/img/9c/8bfe336bb95a028a4fb8130941ff81.png)
[industrial control old horse] detailed design of PLC six way responder system

【深度之眼吴恩达第四期作业班】多元线性回归Linear Regression with multiple variables总结

Vulnhub's dc9 target

AI与元宇宙擦出火花:人类失去的只有枷锁,获得的是全方面的解放

手把手系列---安装SpotBugs、并快速上手使用

Blue Bridge Cup -- Analysis of the second batch of test questions of the 13th session
![[old horse of industrial control] detailed explanation of Siemens PLC s7-300scl programming](/img/da/08f23cfe7f8759b73576eb0638fec5.png)
[old horse of industrial control] detailed explanation of Siemens PLC s7-300scl programming

【工控老马】PLC六路抢答器系统设计详解
随机推荐
Dump (cl\alv\tree\base================================cp|set\items\for\column) when expanding node or clicking toolbar button
Vibration signal generation and processing based on MATLAB Doppler effect
VSLAM特征之线特征&面特征
Pointer reference array element
nor flash 应用层操作
ROS当中的仿真时间以及Bag包操作
施努卡:轮胎自动抓取安装,3D视觉定位,机器人自动抓取
How to permanently set Mysql to utf8 encoding?
Interviewer: why does database connection consume resources? Where are the resources consumed?
[industrial control old horse] detailed design of PLC six way responder system
Kingbasees coping with transaction rollback caused by too fast growth of table age
Detailed design of PLC program control system for washing machine
Common MySQL errors and solutions summarized painstakingly (I)
1032 Sharing
tf. to_ int64
The table cannot be vacuumed because the cursor is open for a long time
【深度之眼吴恩达机器学习作业班第四期】逻辑回归编程实现
Detailed explanation of route (Jiuyang Scripture)
蓝桥杯——13届第二批试题解析
【工控老马】基于西门子S7-200PLC的跑马灯控制系统的设计方案详解