本文比較并總結遍歷數組的四種方式:
for 循環:
1
2
3
4
|
for (let index=0; index < someArray.length; index++) { const elem = someArray[index]; // ··· } |
for-in 循環:
1
2
3
|
for (const key in someArray) { console.log(key); } |
數組方法 .forEach():
1
2
3
|
someArray.forEach((elem, index) => { console.log(elem, index); }); |
for-of 循環:
1
2
3
|
for (const elem of someArray) { console.log(elem); } |
for-of 通常是最佳選擇。我們會明白原因。
for循環 [ES1]
JavaScript 中的 for 循環很古老,它在 ECMAScript 1 中就已經存在了。for 循環記錄 arr 每個元素的索引和值:
1
2
3
4
5
6
7
8
9
10
11
12
|
const arr = [ 'a' , 'b' , 'c' ]; arr.prop = 'property value' ; for (let index=0; index < arr.length; index++) { const elem = arr[index]; console.log(index, elem); } // Output: // 0, 'a' // 1, 'b' // 2, 'c' |
for 循環的優缺點是什么?
- 它用途廣泛,但是當我們要遍歷數組時也很麻煩。
- 如果我們不想從第一個數組元素開始循環時它仍然很有用,用其他的循環機制很難做到這一點。
for-in循環 [ES1]
for-in 循環與 for 循環一樣古老,同樣在 ECMAScript 1中就存在了。下面的代碼用 for-in 循環輸出 arr 的 key:
1
2
3
4
5
6
7
8
9
10
11
12
|
const arr = [ 'a' , 'b' , 'c' ]; arr.prop = 'property value' ; for (const key in arr) { console.log(key); } // Output: // '0' // '1' // '2' // 'prop' |
for-in 不是循環遍歷數組的好方法:
- 它訪問的是屬性鍵,而不是值。
- 作為屬性鍵,數組元素的索引是字符串,而不是數字。
- 它訪問的是所有可枚舉的屬性鍵(自己的和繼承的),而不僅僅是 Array 元素的那些。
for-in 訪問繼承屬性的實際用途是:遍歷對象的所有可枚舉屬性。
數組方法.forEach()[ES5]
鑒于 for 和 for-in 都不特別適合在數組上循環,因此在 ECMAScript 5 中引入了一個輔助方法:Array.prototype.forEach():
1
2
3
4
5
6
7
8
9
10
11
|
const arr = [ 'a' , 'b' , 'c' ]; arr.prop = 'property value' ; arr.forEach((elem, index) => { console.log(elem, index); }); // Output: // 'a', 0 // 'b', 1 // 'c', 2 |
這種方法確實很方便:它使我們無需執行大量操作就能夠可訪問數組元素和索引。如果用箭頭函數(在ES6中引入)的話,在語法上會更加優雅。
.forEach() 的主要缺點是:
- 不能在它的循環體中使用 await。
- 不能提前退出 .forEach() 循環。而在 for 循環中可以使用 break。
中止 .forEach() 的解決方法
如果想要中止 .forEach() 之類的循環,有一種解決方法:.some() 還會循環遍歷所有數組元素,并在其回調返回真值時停止。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
const arr = [ 'red' , 'green' , 'blue' ]; arr.some((elem, index) => { if (index >= 2) { return true ; // 中止循環 } console.log(elem); //此回調隱式返回 `undefined`,這 //是一個偽值。 因此,循環繼續。 }); // Output: // 'red' // 'green' |
可以說這是對 .some() 的濫用,與 for-of 和 break 比起來,要理解這段代碼并不容易。
for-of循環 [ES6]
for-of 循環在 ECMAScript 6 開始支持:
1
2
3
4
5
6
7
8
9
10
|
const arr = [ 'a' , 'b' , 'c' ]; arr.prop = 'property value' ; for (const elem of arr) { console.log(elem); } // Output: // 'a' // 'b' // 'c' |
for-of 在循環遍歷數組時非常有效:
用來遍歷數組元素。
可以使用 await
- 如果有需要,可以輕松地遷移到 for-await-of。
甚至可以將 break 和 continue 用于外部作用域。
for-of 和可迭代對象
for-of 不僅可以遍歷數組,還可以遍歷可迭代對象,例如遍歷 Map:
1
2
3
4
5
6
7
8
9
10
11
|
const myMap = new Map() .set( false , 'no' ) .set( true , 'yes' ) ; for (const [key, value] of myMap) { console.log(key, value); } // Output: // false, 'no' // true, 'yes' |
遍歷 myMap 會生成 [鍵,值] 對,可以通過對其進行解構來直接訪問每一對數據。
for-of 和數組索引
數組方法 .entries() 返回一個可迭代的 [index,value] 對。如果使用 for-of 并使用此方法進行解構,可以很方便地訪問數組索引:
1
2
3
4
5
6
7
8
9
|
const arr = [ 'chocolate' , 'vanilla' , 'strawberry' ]; for (const [index, elem] of arr.entries()) { console.log(index, elem); } // Output: // 0, 'chocolate' // 1, 'vanilla' // 2, 'strawberry' |
總結
for-of 循環的的可用性比 for,for-in 和 .forEach() 更好。
通常四種循環機制之間的性能差異應該是無關緊要。如果你要做一些運算量很大的事,還是切換到 WebAssembly 更好一些。
到此這篇關于JS中循環遍歷數組的四種方式總結的文章就介紹到這了,更多相關JS循環遍歷數組內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://mp.weixin.qq.com/s/3pIv6Q5sUbB8te06LFeUiw