当前位置:网站首页>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);
边栏推荐
- [wechat authorized login] the small program developed by uniapp realizes the function of obtaining wechat authorized login
- Source code of Qiwei automatic card issuing system
- Global and Chinese markets for power over Ethernet (POE) solutions 2022-2028: Research Report on technology, participants, trends, market size and share
- SSO single sign on implementation.
- cookie、session、tooken
- Mitsubishi PLC FX3U pulse axis jog function block (mc_jog)
- I'll teach you to visit Amazon RDS for a year and build a MySQL cloud database (only 10 minutes, really fragrant)
- Variables and constants of go language foundation
- AIX存储管理之逻辑卷的创建及属性的查看和修改
- You probably haven't noticed the very important testing strategy in your work
猜你喜欢

Review notes of compilation principles

Leetcode question brushing: stack and queue 07 (maximum value of sliding window)

Iclr2022 | spherenet and g-spherenet: autoregressive flow model for 3D molecular graph representation and molecular geometry generation

Datawhale community blackboard newspaper (issue 1)

Basis of deep learning neural network
![[eight sorts ④] merge sort, sort not based on comparison (count sort, cardinal sort, bucket sort)](/img/0d/22f3f65ab9422383df9a55d0724d59.jpg)
[eight sorts ④] merge sort, sort not based on comparison (count sort, cardinal sort, bucket sort)

ACM tutorial - quick sort (regular + tail recursion + random benchmark)

What skills does an excellent software tester need to master?

RFID让固定资产盘点更快更准

Minimize the error
随机推荐
Part 29 supplement (XXIX) basis of ECMAScript
gradle
Leetcode skimming: binary tree 03 (post order traversal of binary tree)
Geek DIY open source solution sharing - digital amplitude frequency equalization power amplifier design (practical embedded electronic design works, comprehensive practice of software and hardware)
教你白嫖Amazon rds一年并搭建MySQL云数据库(只需10分钟,真香)
ACM tutorial - quick sort (regular + tail recursion + random benchmark)
[eight sorts ②] select sort (select sort, heap sort)
Deb file installation
SQL injection for Web Security (2)
[eight sorts ④] merge sort, sort not based on comparison (count sort, cardinal sort, bucket sort)
8.8.4-PointersOnC-20220215
2022 safety officer-a certificate examination questions and online simulation examination
To meet the needs of consumers in technological upgrading, Angel water purifier's competitive way of "value war"
Global and Chinese market of wireless chipsets 2022-2028: Research Report on technology, participants, trends, market size and share
Excel search and reference function
[wechat authorized login] the small program developed by uniapp realizes the function of obtaining wechat authorized login
【八大排序④】归并排序、不基于比较的排序(计数排序、基数排序、桶排序)
Common loss function of deep learning
Global and Chinese markets for freight and logistics 2022-2028: Research Report on technology, participants, trends, market size and share
excel查找与引用函数