当前位置:网站首页>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
边栏推荐
- Inheritance (the child constructor inherits the attributes in the parent constructor)
- Scaffold installation
- Recommended system - an embedded learning framework for numerical features in CTR prediction
- Wrote a little webapi knowledge points from 0 to 1
- Three board axe! Help you become an excellent software engineer
- From which dimensions can we judge the quality of code? How to have the ability to write high-quality code?
- redis-基本数据类型(String/list/Set/Hash/Zset)
- 利用用户脚本优化 Yandere/Konachan 站点浏览体验
- numeric学习之iota,accumulate
- 图的遍历-DFS,BFS(代码详解)
猜你喜欢
随机推荐
MVVM model
152. 乘积最大子数组-动态规划
S4/HANA ME21N创建PO 输出控制消息按钮丢失解决方法(切换EDI 输出模式BRF+至NAST模式)
Moment.js
[Muduo] package EventLoop and thread
LeetCode 0136. 只出现一次的数字:异或
Why are there many snapshot tables in the BI system?
Classes and objects (2) (6 default member functions)
[intelligence question] interview intelligence question
意向不到的Dubug妙招
Static agent + dynamic agent
新手哪个券商开户最好 开户最安全
Rendering, filtering (filtering) and sorting of lists
Mongodb update operator (modifier)
POI特效 市场调研
Pytorch data input format requirements and conversion
Grain Academy p98 trample pit e.globalexceptionhandler: null
[QNX hypervisor 2.2 user manual]9.6 GDB
XxE & XML external entity injection utilization and bypass
[nodejs] nodejs create a simple server









