ES5的數組新方法forEach、filter、map、every、some;indexOf和lastIndexOf

        /*
        (1)不改變原數組arr;
        (2)some找到true即停止,every找到false立即停止
        (3)返回值true或false
        (4)數組的方法,是要操作數組中的每一項的,所以item在前,index在後
        (5)而$.each()方法,可以操作數組也可以操作對象,如同for循環,index在前,item在後
        */
        var arr = [1,2,3,4,5];
        var someBackValue = arr.some(function(item, index){//得到一個返回值
            console.log(item);
            return item > 3;//尋找一個true,找到true即停止,且返回true
        })
        var everyBackValue = arr.every(function(item, index){
            console.log(item);
            return item > 3;//尋找一個false,找到false即停止,且返回false
        })
        var filterBackValue = arr.filter(function(item, idnex){
            return item;//只將item添加到filterBackValue數組中
        })
        var mapBackValue = arr.map(function(item, index){
            return item+1;//將return的結果添加到mapBackValue數組中
        })
        arr.forEach(function(item, index){
            console.log(item);//無返回值
        })

 

1. 特點:        

/*

        (1)不改變原數組arr;

        (2)some找到true即停止,every找到false立即停止

        (3)返回值true或false

        (4)數組的方法,是要操作數組中的每一項的,所以item在前,index在後

        (5)而$.each()方法,可以操作數組也可以操作對象,如同for循環,index在前,item在後

        */

2. 兼容性:

IE9+、Firefox2+、Safari3+、Opera9.5+、chrome,即IE9、火狐2、Safari3、歐朋9.5,以及谷歌

3. indexOf和lastIndexOf

這個兩個方法接收兩個參數,要查找的項和(查找的起始點),indexOf從第0位向後查找,而lastIndexOf從數組的末尾向前查找

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章