当前位置:网站首页>Reduce method of array
Reduce method of array
2022-07-25 23:38:00 【An Aquarius procedural ape】
Array of reduce Method
reduce() Method to execute one provided by you in order for each element in the array reducer function , Every time it runs Pass the calculation result of the previous element as a parameter , Finally, the result Summarize to a single return value .
When the callback function is executed for the first time , non-existent “ The result of the last calculation ”, So if you need to index the callback function from the array to 0 The element of starts execution , You need to pass Initial value Otherwise, the index number is 0 The element of will be As an initial value initialValue, The iterator will execute from the second element
grammar :
// Arrow function writing
Array.reduce((perviousValue,currentValue) => {
*** })
Array.reduce((perviousValue,currentValue,currentIndex) => {
*** })
Array.reduce((perviousValue,currentValue,currentIndex,array) => {
*** })
Array.reduce((perviousValue,currentValue,currentIndex,array) => {
*** },initialValue)
// Normal function writing
...
Array.reduce(function(perviousValue,currentValue,currentIndex,array){
*** },initialValue)
Parameters :
Array reduce Method , There are four parameters :
perviousValue: The return value of the last calculation resultcurrentValue: The element being processed in the array . If an initial value is specified , Then the value is array index 0 The elements of , Otherwise 1currentIndex: The index of the element being processed in the array . If an initial value is specified , The starting index is 0, Otherwise 1array: Array of traversed objects
initialValue( Optional ): This parameter is used as the parameter when calling the function for the first timepreviousValueValue ,- If an initial value is specified
initialValue, becurrentValueThe first element of the array will be used ;- otherwise
perviousValueThe first element of the array will be used , andcurrentValueThe second element of the array will be used
Example :
1. Calculates the sum of all elements of an array
// Defines an initial value ( You can also write directly after the parameter )
const initialValue = 0;
// An array is defined
const sum = [0,1,2,3,4,5,6];
// Start the loop calculation
// Receive a return value
const sumTotal = sum.reduce(function(previousValue,currentValue){
return perviousValue + currentValue
},initialValue)
// Print sum
console.log('sumTotal:' + sumTotal) // sumTotal:21
2. Accumulate the values of the object array
Be careful : If you want to accumulate the values contained in the object array ,
mustProvide initialValue Initial value
// Create initial values
const initialValue = 0;
// Define an array
const arr = [{
x:1},{
x:2},{
x:3},{
x:4}];
// Start circularly accumulating under each object in the array x
const sumTotal = arr.reduce(function(previousValue,currentValue){
return previousValue + currentValue.x // object .x Get every one of them x attribute
},initialValue)
// Print sum
console.log('sumTotal:' + sumTotal); // sumTotal:10
边栏推荐
- How does Navicat modify the language (Chinese or English)?
- Query commodity cases (operate data with array addition method) / key points
- [QNX Hypervisor 2.2用户手册]9.7 generate
- Static agent + dynamic agent
- Inheritance (the child constructor inherits the attributes in the parent constructor)
- Canada EE channel
- 学习探索-波浪
- 获取马蜂窝酒店数据
- Promise asynchronous callback function
- ratio学习之ratio_add,ratio_subtract,ratio_multiply,ratio_divide的使用
猜你喜欢
![[JUC] concurrent keyword volatile](/img/80/2f1b33f1e8c87fd4f8806eafb83139.png)
[JUC] concurrent keyword volatile

动态内存管理

Dynamic memory management

WebMvcConfigurationSupport

@Import

Source code of wechat applet for discerning flowers and plants / source code of wechat applet for discerning plants

redis-基本数据类型(String/list/Set/Hash/Zset)

chown: changing ownership of ‘/var/lib/mysql/‘: Operation not permitted

【MUDUO】EventLoop事件循环

Cuteone: a onedrive multi network disk mounting program / with member / synchronization and other functions
随机推荐
BI 系统中为什么会有很多快照表?
热部署和热加载有什么区别?
initializer_ List tool library learning
Query commodity cases (operate data with array addition method) / key points
三板斧!助你成为优秀软件工程师
Source code of wechat applet for discerning flowers and plants / source code of wechat applet for discerning plants
Recursion of function (use recursion to find the factorial of 1-N) (use recursion to find Fibonacci sequence) (use recursion to traverse data)
152. Product maximum subarray - dynamic programming
Several commonly used traversal methods
加拿大EE通道
Qpprogressbar for QT style (QSS) application
意向不到的Dubug妙招
chown: changing ownership of ‘/var/lib/mysql/‘: Operation not permitted
物理防火墙是什么?有什么作用?
《数据密集型应用系统设计》 - 应用系统概览
[test technology performance test LoadRunner] detailed explanation of common functions of LoadRunner
762. 二进制表示中质数个计算置位
Three board axe! Help you become an excellent software engineer
LeetCode 0919. 完全二叉树插入器:完全二叉树的数组表示
PyTorch的数据输入格式要求及转换