当前位置:网站首页>Basic usage of three JS high-order functions --filter---map---reduce
Basic usage of three JS high-order functions --filter---map---reduce
2022-07-02 01:07:00 【KAMHUNCHAN】
1. fillter
Array of filter Method , You need to pass a callback function as a parameter , The parameters of the callback function n, Namely filter The array elements you get every time you cycle through the array , The callback function will return a Boolean, If true, Represents that the element will be placed in the final filter In the returned array .
filter It doesn't change the original array , It returns the filtered new array .
let newarr = arr.filter(function(n){
return n<0
}
)
// Arrow function writing
let newarr = arr.filter(n => n<0)
2. map
Array of map Method , You need to pass a callback function as a parameter , The parameters of the callback function n, Namely map The array elements you get every time you cycle through the array , The return value of the callback function will be put into the final map In the returned array .map The return is an array .
let newarr = arr.map(function(n){
return n*2
})
// Arrow function writing
let newarr = arr.map(n => n*2)
3. reduce
reduce Returns a value
let arr = [1,2,3,4]
let sum = arr.reduce(function(initvalue,n){
//reduce The second parameter 0 Will act as initvalue Pass to callback function
// If reduce The second parameter is not passed , Then the first element of the array will be taken as the first initvalue Pass to callback function ,
// also reduce From the index 1 Where we started callback Method
//n It's every traversal sum Get element value
// Of the callback function return The value of will be as initvalue Pass to the next callback function execution
return initvalue+n
})
console.log(sum);
边栏推荐
- AIX存储管理之卷组属性的查看和修改(二)
- How can programmers better plan their career development?
- Barbie q! How to analyze the new game app?
- cookie、session、tooken
- 【底部弹出-选择器】uniapp Picker组件——底部弹起的滚动选择器
- Advanced skills of testers: a guide to the application of unit test reports
- Leetcode skimming: binary tree 02 (middle order traversal of binary tree)
- Xinniuniu blind box wechat applet source code_ Support flow realization, with complete material pictures
- I'll teach you to visit Amazon RDS for a year and build a MySQL cloud database (only 10 minutes, really fragrant)
- BiLSTM-CRF代码实现
猜你喜欢
测试人进阶技能:单元测试报告应用指南
Collection: comprehensive summary of storage knowledge
To meet the needs of consumers in technological upgrading, Angel water purifier's competitive way of "value war"
Leetcode skimming: binary tree 02 (middle order traversal of binary tree)
Creation of volume group for AIX storage management (I)
Upgraded wechat tool applet source code for mobile phone detection - supports a variety of main traffic modes
[leetcode] number of maximum consecutive ones
【八大排序③】快速排序(动图演绎Hoare法、挖坑法、前后指针法)
Datawhale 社区黑板报(第1期)
教你白嫖Amazon rds一年并搭建MySQL云数据库(只需10分钟,真香)
随机推荐
Comprehensive broadcast of global and Chinese markets 2022-2028: Research Report on technology, participants, trends, market size and share
Variables and constants of go language foundation
XMIND mind map
Excel PivotTable
程序员该如何更好的规划自己的职业发展?
Global and Chinese markets for maritime services 2022-2028: Research Report on technology, participants, trends, market size and share
Schrodinger's Japanese learning applet source code
Mitsubishi PLC FX3U pulse axis jog function block (mc_jog)
How to determine whether the current script is in the node environment or the browser environment?
How to type spaces in latex
Creating logical volumes and viewing and modifying attributes for AIX storage management
970 golang realizes the communication between multithreaded server and client
Global and Chinese markets for context and location-based services 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese markets for supply chain strategy and operation consulting 2022-2028: Research Report on technology, participants, trends, market size and share
Powerful calendar wechat applet source code - support the main mode of doing more traffic
The first "mobile cloud Cup" empty publicity meeting, looking forward to working with developers to create a new world of computing!
Global and Chinese market of ancillary software 2022-2028: Research Report on technology, participants, trends, market size and share
Creation of volume group for AIX storage management (I)
AIX存储管理之卷组属性的查看和修改(二)
How to reflect and solve the problem of bird flight? Why are planes afraid of birds?