| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 
 | <!DOCTYPE html><html lang="en">
 <head>
 <meta charset="UTF-8">
 <title>高阶函数的操作展示</title>
 </head>
 <body>
 
 <script>
 
 
 function get1() {
 return 1;
 }
 
 function plus1(x,f) {
 console.log(x+f());
 }
 plus1(2,get1);
 
 
 console.log("arr.map():");
 var num=[1,2,3,4];
 
 
 var result=num.map(function(x){
 return x*x;
 });
 
 for(var val of result){
 console.log(val);
 }
 
 
 console.log("arr.reduce():");
 var newNum=[1,2,3,4];
 var newResult=newNum.reduce(function(x,y){
 return x+y;
 });
 console.log(newResult);
 
 
 console.log("arr.filter():");
 var stature=[2,3,6,8,1];
 var statureResult=stature.filter(function(x){
 if(x>5)
 return true;
 });
 console.log(statureResult);
 
 
 console.log("arr.sort():");
 /*arr.sort()默认把arr中每个元素当成字符串再排序,所以不明白机制的情况下容易用错
 * 一般用法如下:
 * */
 var order=[2,3,1,5,4];
 var orderResult=order.sort(function(x,y){
 if(x<y){
 
 return 1;
 }
 else if(x==y){
 return 0;
 }
 else{
 return -1;
 }
 });
 console.log(orderResult);
 
 
 console.log("arr.every():");
 
 var weight=[2,3,8,1,5,9];
 var weightResult=weight.every(function(x){
 if(x>5)
 return true;
 });
 console.log(weightResult);
 
 
 console.log("arr.find():");
 
 var height=[5,2,1,2];
 var heightResult=height.find(function (x) {
 if(x===2)
 return true;
 });
 console.log(heightResult);
 
 
 console.log("arr.findIndex():");
 
 var person=["John","Lucy","Bob","Sang"];
 var personResult=person.findIndex(function(x){
 if(x==="Sang")
 return true;
 });
 console.log(personResult);
 
 
 
 
 </script>
 </body>
 </html>
 
 |