当前位置:网站首页>Use of higher order functions
Use of higher order functions
2022-06-23 01:28:00 【The opening of a midsummer night】
The use of higher-order functions
Higher order function – The parameter of the function itself is also a function filter/map/reducefilter The callback function in has a requirement : Must return a boolean value .
If true when , The function will automatically call back this time n Add to a new array ; If you return false, The function will filter out the current n.
Example code used by the function :
// 1.filter Use of functions
const nums = [1,22,11,3,300];
// n yes nums Elements in an array
let newNums = nums.filter(function (n) {
return n < 100; // Will be bigger than the 100 Add elements of to newNums In the new array
})
// 2.map Use of functions
let new2Nums = newNums.map(function(n) {
return n * 2; // take newNums Every element in the array n, multiply 2 after , Return to array
})
// 3.reduce Use of functions
// effect : Sum up all the contents of the array
let total = new2Nmus.reduce(function (preValue, n) {
return preValue + n
},0)
// numsNums = [2,44,22,6]
// The first 1 Time : preValue=0; n=2
// The first 2 Time : preValue=0+2=2; n=44
// The first 3 Time : preValue=2+44=46; n=22
// The first 4 Time :preValue=46+22=68; n=6
Simple writing
let total = newNums.filter(function (n) {
return n < 100;
}).map(function (n) {
return n * 2;
}).reduce(function (preValue, n) {
return preValue + n;
},0)
Easier to write :
let total = newNums.filter(n => n < 100).map(n => n*2).reduce((preValue, n) => preValue + n, 0);
// perhaps map Operations in functions , Can be placed reduce In the function
let total = newNums.filter(n => n < 100).reduce((preValue, n) => preValue + n * 2, 0);
边栏推荐
- A hundred lines of code to realize reliable delay queue based on redis
- Huawei cloud recruits partners in the field of industrial intelligence to provide strong support + commercial realization
- It's still like this
- 使用aggregation API扩展你的kubernetes API
- [hdu] p7058 ink on paper finding the maximum edge of the minimum spanning tree
- [luogu] p1083 [noip2012 improvement group] classroom borrowing (difference)
- C serializabledictionary serialization / deserialization
- Extend your kubernetes API using the aggregation API
- The longest child sequence of the 2019 Blue Bridge Cup
- Population standard deviation and sample standard deviation
猜你喜欢

3DMAX modeling notes (I): introducing 3DMAX and creating the first model Hello World

3D打印微组织

OSPF comprehensive experiment

office2016+visio2016
![[Title Fine brushing] 2023 Hesai FPGA](/img/e8/d9d8c549a4fee529b69ebfc9a9d6ef.png)
[Title Fine brushing] 2023 Hesai FPGA

How to refine permissions to buttons?

MySQL-Seconds_ behind_ Master accuracy error

62. different paths

three. JS simulated driving tour art exhibition hall - creating super camera controller

Day367: valid complete square
随机推荐
[UVM] don't say that your VIP can't use ral model
Add / get and delete cookies
SQL programming task05 job -sql advanced processing
[hdu] p2087 cut cloth strip
使用aggregation API扩展你的kubernetes API
Thead safety experience
[hdu] p7058 ink on paper finding the maximum edge of the minimum spanning tree
A blog allows you to understand the use of material design
Is it safe for Hongyuan futures to open an account? Can Hongyuan futures company reduce the handling fee?
OSPF comprehensive experiment
Local deployment and problem solving of IIS in ArcGIS JS 4.23
Random decoding NLP
Steps to implement a container global component
Binary String
Yyds dry inventory solution sword finger offer: print the binary tree into multiple lines
[luogu] p1083 [noip2012 improvement group] borrow classroom (line segment tree)
Use elk to save syslog, NetFlow logs and audit network interface traffic
Shell 日志与打印输出
JMeter associated login 302 type interface
[cmake command notes]find_ path