当前位置:网站首页>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

边栏推荐
- [matlab] 3D drawing summary
- Database connection to company database denied
- Invalid argument during startup: Failed to open the . conf file: redis-window
- CCF image rotation (Full Score code + problem solving idea) 201503-01
- Greedy interval problem (5)
- 2021-08-05 leetcode notes
- Determine the number of digits of an integer in MATLAB (one line of code)
- Complement (Niuke)
- 1130: find the first character that appears only once
- 1149 dangerous goods packaging (25 points)
猜你喜欢

CCF adjacent number pairs (Full Score code + problem solving ideas + skill summary) 201409-1

ES6 notes

CCF sequence segmentation (Full Score code + problem solving idea) 201509 -1

J - Borg maze (minimum spanning tree +bfs)
![[matlab] 2D drawing summary](/img/de/6bb5385f440a2997dbf9cbb9a756eb.jpg)
[matlab] 2D drawing summary

(Niuke) BFS

Database connection to company database denied

Component communication mode

CCF date calculation (Full Score code + skill summary) February 2, 2015

Svn password forgetting solution
随机推荐
[matlab] 3D drawing summary
1131: genetic correlation
1058 a+b in Hogwarts (20 points)
day02
PS cutting height 1px, Y-axis tiling background image problem
1136: password translation
B. Moamen and k-subarrays (codeforce+ binary search)
CCF elimination games (Full Score code + problem solving ideas + skill summary) February 2, 2015
K high frequency elements before sorting
[extensive reading of papers] multimodal joint attribute prediction and value extraction for e-commerce product
1148 werewolf - Simple Version (20 points)
C language \t usage
1130: find the first character that appears only once
1066 root of AVL tree (25 points)
文本匹配——【NAACL 2022】GPL
Matlab to find prime pairs within 100
Tetris source code (color version)
机械工程师面试的几个问题,你能答上来几个?
Error $(...) size is not a function
K - or unblocked project (minimum spanning tree)