当前位置:网站首页>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);
边栏推荐
- Geek DIY open source solution sharing - digital amplitude frequency equalization power amplifier design (practical embedded electronic design works, comprehensive practice of software and hardware)
- AIX存储管理之逻辑卷的创建及属性的查看和修改
- The first "mobile cloud Cup" empty publicity meeting, looking forward to working with developers to create a new world of computing!
- Zak's latest "neural information transmission", with slides and videos
- RFID makes the inventory of fixed assets faster and more accurate
- 一名优秀的软件测试人员,需要掌握哪些技能?
- Global and Chinese markets for maritime services 2022-2028: Research Report on technology, participants, trends, market size and share
- Global and Chinese market of aircraft MRO software 2022-2028: Research Report on technology, participants, trends, market size and share
- Global and Chinese markets for the application of artificial intelligence in security, public security and national security 2022-2028: Research Report on technology, participants, trends, market size
- Common loss function of deep learning
猜你喜欢

excel数据透视表

King combat power query renamed toolbox applet source code - with traffic main incentive advertisement

Collection: comprehensive summary of storage knowledge

The pain of Xiao Sha

Minimize the error

2022 high altitude installation, maintenance and removal of test question simulation test platform operation

Powerful calendar wechat applet source code - support the main mode of doing more traffic

JMeter做接口测试,如何提取登录Cookie

Advanced skills of testers: a guide to the application of unit test reports

969 interlaced string
随机推荐
什么是商业养老保险?商业养老保险安全靠谱吗?
AIX存储管理之卷组的创建(一)
Part 29 supplement (XXIX) basis of ECMAScript
Leetcode skimming: binary tree 01 (preorder traversal of binary tree)
MySQL winter vacation self-study 2022 12 (4)
【八大排序④】归并排序、不基于比较的排序(计数排序、基数排序、桶排序)
How does schedulerx help users solve the problem of distributed task scheduling?
Barbie q! How to analyze the new game app?
2022 low voltage electrician examination questions and answers
449-原码、补码、反码
[eight sorts ④] merge sort, sort not based on comparison (count sort, cardinal sort, bucket sort)
Creating logical volumes and viewing and modifying attributes for AIX storage management
Otaku wallpaper Daquan wechat applet source code - with dynamic wallpaper to support a variety of traffic owners
Sql--- related transactions
ACM教程 - 快速排序(常规 + 尾递归 + 随机基准数)
JMeter做接口测试,如何提取登录Cookie
Global and Chinese markets for distributed generation and energy storage in telecommunications networks 2022-2028: Research Report on technology, participants, trends, market size and share
【js通过url下载文件】
The pain of Xiao Sha
Two TVs