当前位置:网站首页>Advanced functions of ES6 operation array map (), filter (), reduce()
Advanced functions of ES6 operation array map (), filter (), reduce()
2022-06-30 15:05:00 【Pan Gao】
More , Please visit mine Personal blog .
arr.map() – Update the array
- The original array remains the same
- Callback function parameters :item( Array elements )、index( Sequence )、arr( Original array )
- Loop original array , Use return Operation output item , Return a new array , The new array has the same length as the original array
const originalArr = [1, 2, 3, 4, 5]
const newArr = originalArr.map((item, index, arr) => {
return item * 2 // Multiply each item of the original array by 2, Output new array , The original array remains the same
})
console.info(newArr) // [2, 4, 6, 8, 10]
arr.filter() – Filter array
- The original array remains the same
- Callback function parameters :item( Array elements )、index( Sequence )、arr( Original array )
- Loop original array , Use return Determine whether to output elements , Return a new array , The length of the new array is less than or equal to the original array
const originalArr = [1, 2, 3, 4, 5]
const newArr = originalArr.filter((item, index, arr) => {
return item % 2 == 0 // Output the even items of the original array as a new array , The original array remains the same
})
console.info(newArr) // [2, 4]
arr.reduce() – Overlay array
- The original array remains the same
- Callback function parameters :pre( The initial value is the first item of the array , This is followed by the return value of the last operation )、item( Array elements )、index( Sequence , Subscript from 1 Start )、arr( Original array )
- Loop original array , Use return Operation output , Until the end of the cycle , Returns an output value
const originalArr = [1, 2, 3, 4, 5]
const newArr = originalArr.reduce((pre, item, index, arr) => {
// First cycle :pre:1, item:2, index:1, pre+item:3
// The second cycle :pre:3, item:3, index:2, pre+item:6
// The third cycle :pre:6, item:4, index:3, pre+item:10
// The fourth cycle :pre:10, item:5, index:4, pre+item:15
return pre + item
})
console.info(newArr) // 15
More programming teaching, please pay attention to the official account : Pangao will accompany you to learn programming

边栏推荐
- Programming exercises: special numbers (problem solving ideas + code implementation)
- Double pointer palindrome string
- Zero basic C language learning notes -- first introduction -- 2 data types & variables and constants
- 1133: output family and friends string
- 1082 read number in Chinese (25 points)
- PS cutting height 1px, Y-axis tiling background image problem
- Error $(...) size is not a function
- Scattered knowledge of C language (unfinished)
- 1107 social clusters (30 points)
- Component communication mode
猜你喜欢

LIS error: this configuration section cannot be used in this path

Xiao Sha's pain (thinking problem)

PS dynamic drawing

August 24, 2021 deque queue and stack

CCF numerical sorting (Full Score code + problem solving ideas + skill summary) 201503-2

val_ Loss decreases first and then increases or does not decrease but only increases

Component communication mode
![001 data type [basic]](/img/36/90fdf51a20f8c708b9c38610b377d6.jpg)
001 data type [basic]

CCF call auction (full mark code + problem solving ideas + skill summary) 201412 - 3

Tetris source code (color version)
随机推荐
Win10 backup backup shows that creating a shared protection point on the shadow failed
The difference between queue and stack
Steps for commissioning of vertical machining center
How many questions can you answer for the interview of Mechanical Engineer?
先锋期货安全么?现在期货开户都是哪些流程?期货手续费怎么降低?
001 data type [basic]
文本匹配——【NAACL 2021】AugSBERT
Matlab two-dimensional array example (extract data)
Computer screenshot how to cut the mouse in
Text matching - [naacl 2021] augsbert
高精度CNC加工中心为什么会出现误差?这4个原因你要注意!
August 24, 2021 deque queue and stack
[matlab] 3D drawing summary
2021-07-15Caused by: org. quartz. ObjectAlreadyExistsException: Unable to store Job : ‘DEFAULT. TASK_ 1‘
Summary of C language interview questions
Sum of CCF digits (full mark code + problem solving idea) 201512-1
[untitled]
Determine the number of digits of an integer in MATLAB (one line of code)
JS to realize simple lottery function
[extensive reading of papers] sentimental analysis of online reviews with a hierarchical attention network