当前位置:网站首页>js:Array. Reduce cumulative calculation and array consolidation
js:Array. Reduce cumulative calculation and array consolidation
2022-06-29 08:00:00 【Pengshiyu】
grammar
array.reduce(function(prevValue, currentValue, currentIndex, arr), initialValue)
| Parameters | describe |
|---|---|
| prevValue | It's necessary . Previous value , Or the return value after calculation . |
| currentValue | It's necessary . The current element |
| currentIndex | Optional . Index of the current element |
| arr | Optional . The array object to which the current element belongs . |
| initialValue | Optional . The initial value passed to the function |
Example : Cumulative calculation
let list = [1, 2, 3, 5];
let result = list.reduce((prevValue, currentValue)=>{
return prevValue + currentValue;
}, 0);
console.log(result);
// 11
Example : Merge array
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 ]
Reference resources
https://www.runoob.com/jsref/jsref-reduce.html
边栏推荐
猜你喜欢

Appium 环境搭建
![Detailed explanation of communication principle between [industrial control old horse] single chip microcomputer and Siemens S7-200](/img/56/b300c0c3606dbc328e301092615bff.jpg)
Detailed explanation of communication principle between [industrial control old horse] single chip microcomputer and Siemens S7-200

pycharm的虚拟环境如何共享到jupyter-lab

Segment tree and use

Some examples.

What are the organizational structure and job responsibilities of product managers in Internet companies?

postman预处理/前置条件Pre-request

【深度之眼吴恩达机器学习作业班第四期】Logistic Regression 逻辑回归总结

Common MySQL errors and solutions summarized painstakingly (II)

手撕二叉搜索树(Binary Search Tree)
随机推荐
SQL SERVER 2008 发布订阅到SQL Server 2017避坑指南
Detailed explanation of communication principle between [industrial control old horse] single chip microcomputer and Siemens S7-200
基于Sophus的Ceres优化
C mqtt subscription message
Explanation of swing transformer theory
ROS2中的行为树 BehaviorTree
Software testing
时间操作 - 时间格式转换
Vulnhub's dc6 target
How to share the virtual environment of pycharm to jupyter Lab
征文投稿丨使用轻量应用服务器搭建博客环境
Sonic communication - streaming data processing - window alignment
Vulnhub's dc9 target
Up and down transitions in polymorphism
[repair collection function, update login interface] knowledge payment applet, blog applet, full version open source code, resource realization applet, with 299 whole station resource data
指针引用数组元素
AC自动机
线段树以及使用
Appium environment setup
基础语法 - 位运算