数组的高阶函数

238 阅读1分钟

1. 函数分类

    函数可以分为: 

          系统函数 , 内置函数 , 自定义函数

   1). 系统函数 和 内置函数

        可以直接使用的官方函数

       alert( );

       isNaN( );

       console.log( ); 

       document.write( );

       Boolean( ); 

       Math.pow( );

       prompt( ); 

       confirm( ); 

       parseInt( ); 

       toFixed( ); 

       Number( ); 

       parsefloat();

   2). 自定义函数

2. 数组的高调函数

    ( 都是回调函数 )

   1. some(). 参数是一个回调函数. 如果有一个条件满足就返回true

var arr = [11, 22, 33, 44, 55];       
var res = arr.some(function(el, index, aaa) {             
    return el == 33; //true  有则返回true,否则返回false        
})        
console.log(res);

   2. every(). 参数是一个回调函数. 必须所有的条件满足就是返回true

var arr = [11, 22, 33, 44, 55];        
var res = arr.every(function (el, index, aaa) {            
    return el > 30; //false  还有条件没有满足        
})        
console.log(res);

   3. filter(). 过滤. 参数是一个回调函数, 结果是返回一个数组.

var arr = [11, 22, 33, 44, 55];        
var res = arr.filter(function(el, index, aaa) {            
    if (el > 30) {                
       return el;             
    }        
})        
console.log(res);

4. forEach(). 用于遍历

arr.forEach(function (el, index, aaa) {            
    console.log(el, index, aaa);        
})

5. map(). 是一个回调函数, 也是用于遍历, 但是有返回值, 返回一个数组

var arr = [11, 22, 33, 44, 55];        
var res = arr.map (function(el, index, aaa) {            
    return index * 10;        
})        
console.log(res);

6. find(). 是一个回调函数, 表示被查到的下标的值

var arr = ["周杰", "刘德华", "刘德军", "周杰伦", "刘杰"];        
var res = arr.find(function(el, index, aaa) {            
    return index==2;        
})        
console.log(res);

7. findIndex(). 是一个回调函数, 返回查询的下标

var arr = ["周杰", "刘德华", "刘德军", "周杰伦", "刘杰"];        
var res = arr.findIndex(function (el, index, aaa) {            
    return el == "周杰伦";  // 得到下标        
})        
console.log(res);