高阶函数

对于函数接受函数提供了便捷的操作

1
2
3
4
5
6
7
8
9
function theHighter(x,y,f){
return f(x)+f(y)
}

var x=3
var y=5
//函数名称调用和直接调用的差别!
//千万注意
console.log(theHighter(x,y,Math.floor))

Map&Reduce

高阶函数主要的作用就是封装部分低阶函数的操作,进而进行非常优质的理解操作

关于Map的性质

1
2
3
4
5
6
7
function pow(x){
return x*x
}
var arr=[1,3,5,7,9]
console.log(arr.map(pow))
//内置复杂的数据类型转换操作
console.log(arr.map(String))

关于Reduce的性质

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
var arr2=[3,24324,32]
console.log(arr2.reduce(function (x,y){
return x + y;
}))
var arr3=[111]
console.log(arr3.reduce(function (x,y) {
return x+y
//如果不写自动归零
},6))
console.log(arr2)
//reduce求积

var theMutiply=[1,2,3,21,3]
console.log(theMutiply.reduce(function(x,y){
return x*y;
}))
//[x1, x2, x3, x4].reduce(f) = f(f(f(x1, x2), x3), x4)
//这里是关键,如何调用理解是非常必要的!
console.log(theMutiply.reduce(function(x,y){
return x*10+y;
}))

这里是reduce中参数性质计算的本质,知道这个就可以完全的理解字符串转换为数字的理解操作了

1
//[x1, x2, x3, x4].reduce(f) = f(f(f(x1, x2), x3), x4)

DEMO演示

1
2
3
4
5
6
7
8
9
10
11
12
//Number2String
var string2="13579"
var theStringArray=[]
for (const stringElement of string2) {
theStringArray.push(stringElement)
}
var theNumberArray=theStringArray.map(Number)
console.log(theNumberArray)
var number= theNumberArray.reduce(function(x, y) {
return x * 10+y;
})
console.log(number)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//请把用户输入的不规范的英文名字,变为首字母大写,其他小写的规范名字。输入:['adam', 'LISA', 'barT'],输出:['Adam', 'Lisa', 'Bart']。
function theStandard(arr){
var theNewlyLowerCaseArray=[]
for (const arrElement of arr) {
theNewlyLowerCaseArray.push(arrElement.toLowerCase())
}
var theNewArray=[]
var theFirestNode=theNewlyLowerCaseArray.map(function (x) {
return x.substring(0,1).toUpperCase();
})
var theRestNode=theNewlyLowerCaseArray.map(function(x) {
return x.substring(1)
})
for (var i=0;i<theFirestNode.length; i++){
theNewArray.push(theFirestNode[i]+theRestNode[i])
}
return theNewArray
}
console.log(theStandard(['adam', 'LISA', 'barT']))

FILTER(过滤器)

主要应用场景,仅仅用于过滤集合中的某些元素

设置过滤条件,注意过滤条件之间的差别,过滤条件只有Boolean值操作才会进行,所以一般条件设置都只是布尔值操作

1
2
3
4
5
var thefilter=[2,2,31,23,21,3,214,3,4,5,3254,7,8,8,945,352,9,5]
var theNewArray=thefilter.filter(function (x) {
return x%2==0
})
console.log(theNewArray)
1
2
3
4
5
6
7
var theTrimfilter=["thisthings","",null,undefined,""]
var theNewArray=thefilter.filter(function(x) {
//ie9下没有这个方法,自然会报错!
//仍然还是基于浏览器进行的操作理解啊
return x && x.trim()
})
console.log(theNewArray)
1
2
3
4
5
6
7
8

var theStringArray=["jack","queen","king","jack","queen","king","master"]
//对于回调函数的理解就是简单的操作进行的!
var theNewArray2=theStringArray.filter(function (element,index,self) {
//不相等的理解操作,如何实现也是非常必要的。这里返回的是真值操作
return self.indexOf(element)==index
})
console.log(theNewArray2)

小demo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//筛选素数

var arr=[]
for (var i = 1; i < 600; i++) {
arr.push(i)
}
//筛选质数的很多操作也是令人沉迷
var theSuArray=arr.filter(function (element,index,self){
if (element==1){
return false
}
for (var i = 2; i <element;i++ ) {
if (element%i==0) {
return false
}
}
return true
})
console.log(theSuArray)

Sort

对于此处的sort高阶函数理解,将默认的比较规则提升为了自定义的语法规则,这样的操作实现是非常优质的!,并且,对于高阶函数的操作来讲,简化了核心代码,让代码整体显得比较简介,不需要太多的冗余代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
var theStringArray=["jack","queen","king","A","B"]
console.log(theStringArray.sort())

//the into sort
var Test= ['Google', 'apple', 'Microsoft',"jack","king","queen","king"]
console.log(Test.sort())
Test.sort(function(x,y) {
//全转换操作
x1=x.toUpperCase()
y1=y.toUpperCase()
if (x1>y1){
//那么是否我能理解为一种优先级的操作理解!
return 1
}if (x1 < y1) {
return -1
}
return 0
})
console.log(Test)


Array

1
2
3
4
5
6
7
8
var theNewArray=["jack","queen","King"]
console.log(theNewArray.every(function (x) {
return x.toLowerCase()==x
}))
var theNewArray2=theNewArray.map(function(x) {
return x.toLowerCase()
})
console.log(theNewArray2)
1
2
3
4
//查找符合条件的第一个元素操作
console.log(theAnotherArray.find(function (x) {
return x.length>5
}))
1
2
3
4
//查找符合条件的第一个元素的下标
console.log(theAnotherArray.findIndex(function(x) {
return x.length>5
}))
1
2
3
4
5
//foreach 直接遍历原型参数理解
theAnotherArray.forEach(console.log)
theAnotherArray.forEach(function (element,index,array) {
//两种方法都可以
})

image-20231001192257311