当前位置:网站首页>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 1undefinedAt 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 1undefinedCode block two:
[4,5,1].reduce((a,b) => {console.log(a,b);return a+b;})console result ==>4 59 110Code 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 1It 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
边栏推荐
- 『Another Redis DeskTop Manager』用了这款Redis可视化工具,分析效率提升12倍
- typescript90-使用类型文件声明类型
- JDBC技术(一)——一个简单的JDBC测试
- MT4/MQ4L入门到精通EA教程第二课-MQL语言常用函数(二)-账户信息常用功能函数
- 力扣刷题记录--常用功能函数
- Go-11 - Process Control
- Grid布局介绍
- MT4/MQL4入门到精通EA教程第一课-MQL语言常用函数(一)OrderSend()函数
- Cmake 报错 Could not find a package configuration file provided by “OpenCV“
- Go-10-模块与包
猜你喜欢

etcd实现大规模服务治理应用实战

考研人总结的时间管理7大忌,你中了几条?

使用百度EasyDL实现智能垃圾箱

pytorch相关知识点总结

配置文件的读取-TOML

Introduction to LVGL (based on v8.1-8.2)
![[Cellular Automata] Simulation of emergency evacuation of disaster personnel under social force factors based on cellular automata with matlab code attached](/img/c3/77fba03e3615485fbbed43d1bc22be.png)
[Cellular Automata] Simulation of emergency evacuation of disaster personnel under social force factors based on cellular automata with matlab code attached

全文翻译:欧盟第29条数据保护工作组 数据保护官指南

『Another Redis DeskTop Manager』用了这款Redis可视化工具,分析效率提升12倍

2022 PMP Project Management Certification Exam Registration Guide (1)
随机推荐
Go-12-结构体
Go-8-Gin framework
How to install ngrok in Synology system (Synology 6.X version)
《LC刷题总结》——贪心
Use of torchversion.transforms
Etcd realize large-scale application service management of actual combat
力扣刷题记录3.1-----977. 有序数组的平方
UsernameAuthenticationFilter授权成功后调用AuthenticationSuccessHandler时的解析
Go-8-Gin框架
2022 PMP Project Management Certification Exam Registration Guide (1)
OpenSceneGraph3.5.1编译
显著性检验--学习笔记
Observer pattern
2022/8/8 比赛思维+状压dp
数字孪生+燃气管理,开启智慧燃气管理新模式
Phenomenon 1 during RF debugging
RF调试过程中现象一
Image denoising based on edge enhancement Diffusion 】 (cEED) and Coherence Enhancing coursing together (cCED) filter to realize image denoising matlab code
gstreamer 记录
[Signal denoising] Based on Sage-Husa adaptive Kalman filter to realize the suppression of ocean wave magnetic field noise and the generation of ocean wave magnetic field noise with matlab code