当前位置:网站首页>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 construction operation example
- [extensive reading of papers] multimodal attribute extraction
- [extensive reading of papers] multimodal joint attribute prediction and value extraction for e-commerce product
- Judgment of deep learning experiment results
- Programming exercises: whole point and circle (solution ideas and code implementation)
- [extensive reading of papers] analyzing connections between user attributes, images, and text
- Effect of shadow around the block after mouse over
- How does hbuilder display in columns?
- Basic learning notes of C language
- Searching for single element in dichotomy
猜你喜欢

Zero basic C language learning notes -- first introduction -- 2 data types & variables and constants

2021-05-12

Solve the problem that codeblocks20.03 on win11 cannot run for the first time

2021-07-14 mybaitsplus
![[extensive reading of papers] multimodal attribute extraction](/img/ec/546c107ac0d31deded7ca94fdf0e2d.jpg)
[extensive reading of papers] multimodal attribute extraction

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

How does hbuilder display in columns?

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

How to realize selective screen recording for EV screen recording

Matlab judge palindrome number (only numbers)
随机推荐
CCF drawing (full mark code + problem solving ideas + skill summary) February 2, 2014
M - smooth engineering continuation (minimum spanning tree)
1137: encrypted medical record
Judgment of deep learning experiment results
立式加工中心调试的步骤
[matlab] 2D drawing summary
Basic requirements for tool use in NC machining of vertical machining center
Summary of C language interview questions
Complement (Niuke)
2021-07-15Caused by: org. quartz. ObjectAlreadyExistsException: Unable to store Job : ‘DEFAULT. TASK_ 1‘
How to program and process such parts?
Working principle and fault treatment of cutting cylinder in CNC machining center
CCF image rotation (Full Score code + problem solving idea) 201503-01
Searching for single element in dichotomy
CCF string matching (Full Score code + problem solving ideas + skill summary) March 3, 2014
Examples of bubble sorting and matrix element screening in MATLAB
K - or unblocked project (minimum spanning tree)
How to realize selective screen recording for EV screen recording
1149 dangerous goods packaging (25 points)
How many questions can you answer for the interview of Mechanical Engineer?