当前位置:网站首页>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
- Promise和模块块化编程
- 【八大排序④】归并排序、不基于比较的排序(计数排序、基数排序、桶排序)
- From 20s to 500ms, I used these three methods
- Leetcode skimming: stack and queue 04 (delete all adjacent duplicates in the string)
- Summary of Aix storage management
- Geek DIY open source solution sharing - digital amplitude frequency equalization power amplifier design (practical embedded electronic design works, comprehensive practice of software and hardware)
- Sql--- related transactions
- Comprehensive broadcast of global and Chinese markets 2022-2028: Research Report on technology, participants, trends, market size and share
- Otaku wallpaper Daquan wechat applet source code - with dynamic wallpaper to support a variety of traffic owners
猜你喜欢

To meet the needs of consumers in technological upgrading, Angel water purifier's competitive way of "value war"
![[bottom pop-up selector] uniapp picker component - scroll selector popped up at the bottom](/img/d4/9d27b29080ce83004aa875a499de9b.png)
[bottom pop-up selector] uniapp picker component - scroll selector popped up at the bottom

Geek DIY open source solution sharing - digital amplitude frequency equalization power amplifier design (practical embedded electronic design works, comprehensive practice of software and hardware)

Viewing and modifying volume group attributes of Aix storage management (II)

Output results of convolution operation with multiple tensors and multiple convolution kernels

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

You probably haven't noticed the very important testing strategy in your work

Develop a simple login logic based on SSM

excel数据透视表

Leetcode question brushing: stack and queue 07 (maximum value of sliding window)
随机推荐
关于ASP.NET CORE使用DateTime日期类型参数的一个小细节
cookie、session、tooken
Bc35 & bc95 onenet mqtt (old)
Creating logical volumes and viewing and modifying attributes for AIX storage management
Schrodinger's Japanese learning applet source code
8.8.4-PointersOnC-20220215
[WesternCTF2018]shrine writeup
【八大排序③】快速排序(动图演绎Hoare法、挖坑法、前后指针法)
AIX存储管理之逻辑卷的创建及属性的查看和修改
How to type spaces in latex
449 original code, complement code, inverse code
[wechat authorized login] the small program developed by uniapp realizes the function of obtaining wechat authorized login
970 golang realizes the communication between multithreaded server and client
[leetcode] number of maximum consecutive ones
Leetcode question brushing: stack and queue 07 (maximum value of sliding window)
Export default the exported object cannot be deconstructed, and module Differences between exports
Node -- egg creates a local file access interface
一名优秀的软件测试人员,需要掌握哪些技能?
"C zero foundation introduction hundred knowledge hundred examples" (73) anonymous function -- lambda expression
Viewing and modifying volume group attributes of Aix storage management (II)