当前位置:网站首页>Simple example of .reduce()
Simple example of .reduce()
2022-08-09 02:05:00 【dal mudou】
Seek the output of [1,2,3].reduce((a,b) => console.log(a,b))?
The reason for writing this article is to see ↑ this topic, the print result is
4 5undefined 1undefined
At that time, I was very puzzled how to print the strange result of undefined
Let's take a look at the definition of reduce:
Definition and usage
The reduce() method accepts a function as an accumulator, and each value in the array (from left to right) is reduced to a value.
reduce() can be used as a higher-order function to compose functions.
Note: reduce() will not execute the callback function for an empty array.
Syntax
array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
parameters | Description |
---|---|
total | Required.The initial value, or the return value after the calculation ends. |
currentValue | Required.current element |
currentIndex | Optional.The 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 |
--------------------------------------------The above is the introduction of reduce------------------------------
You can see some examples below
Code block one:
[4,5,1].reduce((a,b) => console.log(a,b);)console result ==>4 5undefined 1undefined
Code block two:
[4,5,1].reduce((a,b) => {console.log(a,b);return a+b;})console result ==>4 59 110
Code block three:
========> 1:[4,5,1].reduce((a,b) => {console.log(a,b);return a+b;},0)console result ==>0 44 59 1=======> 2:[4,5,1].reduce((a,b) => {console.log(a,b);return a+b;},5)console result ==>5 49 514 1
It can be seen that in the code block 1 that prints undefined, there is no return, and in the reduce syntax, if you want to add, you need to set the return value –>total |Required.The initial value, or the return value after the calculation is complete .Since there is no return operation in this code block, the value of undefined will appear
The difference between code block 2 and code block 3-1 is that the initialValue parameter is added to the third, and the function of this parameter is to set the value passed to the function by Initial value , when not set, initialValue will start from the subscript value with index 1 (such as code block 2)
The difference can also be seen in code block three-1 and code block three-2, when initialValue is set to 0, it has no effect on the result of the data, and when it is set to 5, that is, set 5 as the initial value for the addition operation
Of course, in addition to simple mathematical operations such as addition, reduce has more advanced usage methods, such as array deduplication, array merging, etc. The following are the portals of other bloggers:
Other advanced operations of reduce
边栏推荐
- 增额终身寿险哪家最好呢?真的安全吗?
- 力扣刷题记录3.1-----977. 有序数组的平方
- composer的使用记录
- Design of Go-7-RESTful API
- Analysis of when AuthenticationSuccessHandler is called after UsernameAuthenticationFilter is authorized successfully
- class path resource [bean.xml] cannot be opened because it does not 错误解决方案
- mysql连接超过八小时报错
- Grid布局介绍
- 力扣刷题记录4.1-----209. 长度最小的子数组
- LeetCode每日一题:搜索插入位置 (均1200道)方法:二分查找
猜你喜欢
How js implements array deduplication (7 kinds)
LeetCode每日两题01:二分查找 (均1200道)
The last exam before the NPDP revision!caution
Codeforces Round #809 (Div. 2)A~D1
How SEMRush finds keywords for advertising
pytorch相关知识点总结
力扣刷题记录1.5-----367. 有效的完全平方数
NPDP改版前最后一次考试!请注意
2022PMP项目管理认证考试报考指南(1)
Use of torchversion.transforms
随机推荐
Codeforces Round #809 (Div. 2)A~D1
力扣刷题记录3.1-----977. 有序数组的平方
Go-11 - Process Control
[C language brush questions] Application of fast and slow pointers in linked lists
MAYA发动机建模
Qt中QFile、QByteArray QDataStream和QTextStream区别
Use of torchversion.transforms
虹科技术|如何阻止供应链攻击?
2020.12.4 log
Latex示例参考
pytorch相关知识点总结
全文翻译:EDPB 基于设计和默认的数据保护指南
MT4/MQL4入门到精通EA教程第一课-MQL语言常用函数(一)OrderSend()函数
Cmake 报错 Could not find a package configuration file provided by “OpenCV“
JDBC技术(三)——使用Druid数据库连接池测试
Educational Codeforces Round 132 (Rated for Div. 2)
增额终身寿险哪家最好呢?真的安全吗?
Which is the best increased whole life insurance?Is it really safe?
LeetCode每日两题02:轮转数组 (均1200道)
Go-11-流程控制