JS 數組遍歷的方法

整理了一下數組遍歷的方法數組


一、for循環

let arr = [1,2,3,4]
for(let j = 0,len=arr.length; j < len; j++) {
    console.log(arr[j]);
}

clipboard.png

二、forEach循環

//1 沒有返回值
arr.forEach((item,index,array)=>{
    console.log(index+':'+arr[index]);
})
//參數:value數組中的當前項, index當前項的索引, array原始數組;
//數組中有幾項,那麼傳遞進去的匿名回調函數就須要執行幾回;

clipboard.png

三、map循環

map的回調函數中支持return返回值;
並不影響原來的數組,只是至關於把原數組克隆一份,把克隆的這一份的數組中的對應項改變了;函數

var ary = [12,23,24,42,1];
var res = ary.map(function (item,index,ary ) {
    return item*10;
})
console.log(res);//-->[120,230,240,420,10];  原數組拷貝了一份,並進行了修改
console.log(ary);//-->[12,23,24,42,1];  原數組並未發生變化

clipboard.png

四、for Of 遍歷

能夠調用break、continue語句測試

var myArray= [12,23,24,42,1];
for (var value of myArray) {
console.log(value);
}

clipboard.png

五、filter遍歷

不會改變原始數組,返回新數組spa

var arr = [
  { id: 1, value: 2 },
  { id: 2, value: 4 },
  { id: 2, value: 7 },
]
let newArr = arr.filter(item => item.value>2);
console.log(newArr ,arr )

clipboard.png

六、every遍歷

every()是對數組中的每一項運行給定函數,若是該函數對每一項返回true,則返回true。若是返回false,就退出遍歷code

var arr = [ 1, 2, 3, 4, 5, 6 ];
if(arr.every( function( item, index, array ){
        return item > 3;
   })){
        console.log("知足條件,每個都大於3" );
    }else{
        console.log("不知足條件,不是每個都大於3" );
    }

clipboard.png

七、some遍歷

some()是對數組中每一項運行指定函數,若是該函數對任一項知足條件,則返回true,就退出遍歷;不然返回false。對象

var arr = [ 1, 2, 3, 4, 5, 6 ];
if(arr.some( function( item, index, array ){
        return item > 3;
   })){
        console.log("");
    }else{
        console.log("不知足條件,沒有大於3的" );
    }

clipboard.png

八、reduce

reduce 爲數組中的每個元素依次執行回調函數,不包括數組中被刪除或從未被賦值的元素,接受四個參數:初始值(或者上一次回調函數的返回值),當前元素值,當前索引,調用 reduce 的數組。索引

var total = [0,1,2,3,4].reduce((a, b)=>a + b); //10
console.log(total)

clipboard.png

九、reduceRight

reduceRight()方法的功能和reduce()功能是同樣的,不一樣的是reduceRight()從數組的末尾向前將數組中的數組項作累加。
reduceRight()首次調用回調函數callbackfn時,prevValue 和 curValue 能夠是兩個值之一。若是調用 reduceRight() 時提供了 initialValue 參數,則 prevValue 等於 initialValue,curValue 等於數組中的最後一個值。若是沒有提供 initialValue 參數,則 prevValue 等於數組最後一個值, curValue 等於數組中倒數第二個值。ip

var arr = [0,1,2,3,4];
arr.reduceRight(function (preValue,curValue,index,array) {
    console.log(preValue ,curValue)
    return preValue + curValue;
}); // 10

clipboard.png

十、find

find()方法返回數組中符合測試函數條件的第一個元素。不然返回undefinedelement

let name= [
    {
        name: '張三',
        gender: '男',
        age: 20
    },
    {
        name: '王小毛',
        gender: '男',
        age: 20
    },
    {
        name: '李四',
        gender: '男',
        age: 20
    }
];
function getStu(element){
   return element.name == '李四'
}
name.find(getStu)

clipboard.png

十一、findIndex

對於數組中的每一個元素,findIndex 方法都會調用一次回調函數(採用升序索引順序),直到有元素返回 true。只要有一個元素返回 true,findIndex 當即返回該返回 true 的元素的索引值。若是數組中沒有任何元素返回 true,則 findIndex 返回 -1。
findIndex 不會改變數組對象。get

[1,2,3].findIndex(function(x) { x == 2; });

[1,2,3].findIndex(x => x == 4);

[1,2,3].findIndex(x => x == 3);

clipboard.png

十二、ES6 新方法keys,values,entries

ES6 提供三個新的方法 —— entries(),keys()和values() —— 用於遍歷數組。它們都返回一個遍歷器對象,能夠用for...of循環進行遍歷,惟一的區別是keys()是對鍵名的遍歷、values()是對鍵值的遍歷,entries()是對鍵值對的遍歷

for (let index of ['a', 'b'].keys()) {
console.log(index);
}
// 0
// 1
for (let elem of ['a', 'b'].values()) {
console.log(elem);
}
// 'a'
// 'b'
for (let [index, elem] of ['a', 'b'].entries()) {
console.log(index, elem);
}
// 0 "a"
// 1 "b"

clipboard.png