当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
考研人总结的时间管理7大忌,你中了几条?
typescript91-添加任务基本实现
最新豆瓣top250爬虫案例代码分析[注释齐全]
『Another Redis DeskTop Manager』用了这款Redis可视化工具,分析效率提升12倍
右键新建缺少word、excel选项问题处理
HCIP-R&S By Wakin自用笔记(2)OSPF之OSPF回顾、虚连接
《LC刷题总结》——贪心
eladmin容器部署超详细过程
基于机器学习之模型树短期负荷预测(Matlab代码实现)
《LC刷题总结》—— 二叉树
gstreamer 记录
Use of torchversion.transforms
makefile file compilation
2020.10.13开发日志
数字孪生+燃气管理,开启智慧燃气管理新模式
虹科技术|如何阻止供应链攻击?
MAYA发动机建模
PMP有什么答题技巧?
js实现数组去重的方式(7种)
Z-Game on grid