文章主要來源:
我們用更簡潔的語法(比如內置函數)遍歷數組,從而消除循環結構。
forEach、map、filter、reduce、every、some 不可以使用 break 和 continue ,因為在function中解決了閉包陷阱的問題
for、for...in、for...of、while可以使用?break、continue
用于遍歷數組元素使用:for(),forEach(),map(),for...of
用于循環對象屬性使用:for...in?
1.for循環
使用臨時變量將長度緩存起來,避免重復獲取數組長度,當數組較大時優化效果比較明顯。
缺點:這種寫法比較麻煩
for(j = 0,len=arr.length; j < len; j++) {
}
2.foreach循環
遍歷數組中的每一項,沒有返回值,可以不用知道數組長度,對原數組沒有影響,不支持IE
缺點:這種寫法的問題在于,無法中途跳出forEach循環,break命令或return命令都不能奏效。
//1 沒有返回值
arr.forEach((item,index,array)=>{
????//執行代碼
})
//參數:item數組中的當前項, index當前項的索引, array原始數組;
//數組中有幾項,那么傳遞進去的匿名回調函數就需要執行幾次;
3.map循環
map函數,遍歷數組每個元素,并回調操作,需要返回值,返回值組成新的數組,原數組不變。
每個元素都是回調函數返回的值。
arr.map(function(value,index,array){
//do something
return?XXX
})
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];? 原數組并未發生變化
4.forof遍歷
可以正確響應break、continue和return語句
for?(var?value of myArray) {
console.log(value);
}
5.filter遍歷
返回一個新數組,數組的元素是原數組中通過測試的元素(就是回調函數返回 true 的話,對應的元素會進入新數組), 原數組不變。
var?arr = [
??{ id: 1, text:?'aa', done:?true?},
??{ id: 2, text:?'bb', done:?false?}
]
console.log(arr.filter(item => item.done))
轉為ES5
arr.filter(function?(item) {
??return?item.done;
});
var?arr = [73,84,56, 22,100]
var?newArr = arr.filter(item => item>80)?//得到新數組 [84, 100]
console.log(newArr,arr)
6.some遍歷
some()類似于filter(),不同的是返回值為Boolean,只要有一個值滿足即立刻返回true,不再繼續執行,否則返回false。
some()是對數組中每一個元素運行指定函數,如果該函數對任一個元素返回true,則返回true。
var?arr = [ 1, 2, 3, 4, 5, 6 ];?
????console.log( arr.some(?function( item, index, array ){?
????????return?item > 3;?
????}));?
true
7.every遍歷
every()類似于some(),不同的是找到符合條件的值會繼續執行,如果每個值都滿足條件才會返回true,否則就是false。
every()是對數組中的每一個元素運行指定函數,如果該函數對每一個元素返回true,則返回true。
var?arr = [ 1, 2, 3, 4, 5, 6 ];?
console.log( arr.every(?function( item, index, array ){?
????????return?item > 3;?
????}));?
false
8.reduce
reduce()?方法接收一個函數作為累加器(accumulator),數組中的每個值(從左到右)開始縮減,最終為一個值。
var?total = [0,1,2,3,4].reduce((a, b)=>a + b);?//10
reduce接受一個函數,函數有四個參數,分別是:上一次的值,當前值,當前值的索引,數組
[0, 1, 2, 3, 4].reduce(function(previousValue, currentValue, index, array){
?return?previousValue + currentValue;
});
9.reduceRight
reduceRight()方法的功能和reduce()功能是一樣的,不同的是reduceRight()從數組的末尾向前將數組中的數組項做累加。
reduceRight()首次調用回調函數callbackfn時,prevValue和curValue可以是兩個值之一。如果調用reduceRight()時提供了initialValue參數,則prevValue等于initialValue,curValue等于數組中的最后一個值。如果沒有提供initialValue參數,則prevValue等于數組最后一個值,curValue等于數組中倒數第二個值。
var?arr = [0,1,2,3,4];
arr.reduceRight(function?(preValue,curValue,index,array) {
????return?preValue + curValue;
});?// 10
10.find
find()方法返回數組中符合測試函數條件的第一個元素。否則返回undefined?
var?stu = [
????{name:?'張三',gender:?'男',age: 20},
????{name:?'王小毛',gender:?'男',age: 20},
????{ name:?'李四',gender:?'男', age: 20}
]
function?getStu(element){
???return?element.name ==?'李四'
}
stu.find(getStu)
//返回結果為
//{name: "李四", gender: "男", age: 20}
ES6方法
stu.find((element) => (element.name ==?'李四'))
11.findIndex
對于數組中的每個元素,findIndex方法都會調用一次回調函數(采用升序索引順序),直到有元素返回true。只要有一個元素返回 true,findIndex立即返回該返回 true 的元素的索引值。如果數組中沒有任何元素返回 true,則findIndex返回 -1。
findIndex不會改變數組對象。
[1,2,3].findIndex(function(x) { x == 2; });
// Returns an index value of 1.
[1,2,3].findIndex(x => x == 4);
// Returns an index value of -1.
12.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"