自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

JS中循環(huán)遍歷數(shù)組方式總結(jié)

開(kāi)發(fā) 前端
本文比較并總結(jié)遍歷數(shù)組的四種方式:for 循環(huán)、for-in 循環(huán)、數(shù)組方法 .forEach()、for-of 循環(huán),for-of 通常是最佳選擇。我們會(huì)明白原因。

[[380935]]

本文比較并總結(jié)遍歷數(shù)組的四種方式:

  • for 循環(huán):
    1. for (let index=0; index < someArray.length; index++) { 
    2.   const elem = someArray[index]; 
    3.   // ··· 
  • for-in 循環(huán):
    1. for (const key in someArray) { 
    2.   console.log(key); 
  • 數(shù)組方法 .forEach():
    1. someArray.forEach((elem, index) => { 
    2.   console.log(elem, index); 
    3. }); 
  • for-of 循環(huán):
    1. for (const elem of someArray) { 
    2.   console.log(elem); 

for-of 通常是最佳選擇。我們會(huì)明白原因。

for循環(huán) [ES1]

JavaScript 中的 for 循環(huán)很古老,它在 ECMAScript 1 中就已經(jīng)存在了。for 循環(huán)記錄 arr 每個(gè)元素的索引和值:

  1. const arr = ['a', 'b', 'c']; 
  2. arr.prop = 'property value'
  3.  
  4. for (let index=0; index < arr.length; index++) { 
  5.   const elem = arr[index]; 
  6.   console.log(index, elem); 
  7.  
  8. // Output: 
  9. // 0, 'a' 
  10. // 1, 'b' 
  11. // 2, 'c' 

for 循環(huán)的優(yōu)缺點(diǎn)是什么?

  • 它用途廣泛,但是當(dāng)我們要遍歷數(shù)組時(shí)也很麻煩。
  • 如果我們不想從第一個(gè)數(shù)組元素開(kāi)始循環(huán)時(shí)它仍然很有用,用其他的循環(huán)機(jī)制很難做到這一點(diǎn)。

for-in循環(huán) [ES1]

for-in 循環(huán)與 for 循環(huán)一樣古老,同樣在 ECMAScript 1中就存在了。下面的代碼用 for-in 循環(huán)輸出 arr 的 key:

  1. const arr = ['a', 'b', 'c']; 
  2. arr.prop = 'property value'
  3.  
  4. for (const key in arr) { 
  5.   console.log(key); 
  6.  
  7. // Output: 
  8. // '0' 
  9. // '1' 
  10. // '2' 
  11. // 'prop' 

for-in 不是循環(huán)遍歷數(shù)組的好方法:

  • 它訪(fǎng)問(wèn)的是屬性鍵,而不是值。
  • 作為屬性鍵,數(shù)組元素的索引是字符串,而不是數(shù)字。
  • 它訪(fǎng)問(wèn)的是所有可枚舉的屬性鍵(自己的和繼承的),而不僅僅是 Array 元素的那些。

for-in 訪(fǎng)問(wèn)繼承屬性的實(shí)際用途是:遍歷對(duì)象的所有可枚舉屬性。

數(shù)組方法.forEach()[ES5]

鑒于 for 和 for-in 都不特別適合在數(shù)組上循環(huán),因此在 ECMAScript 5 中引入了一個(gè)輔助方法:Array.prototype.forEach():

  1. const arr = ['a', 'b', 'c']; 
  2. arr.prop = 'property value'
  3.  
  4. arr.forEach((elem, index) => { 
  5.   console.log(elem, index); 
  6. }); 
  7.  
  8. // Output: 
  9. // 'a', 0 
  10. // 'b', 1 
  11. // 'c', 2 

這種方法確實(shí)很方便:它使我們無(wú)需執(zhí)行大量操作就能夠可訪(fǎng)問(wèn)數(shù)組元素和索引。如果用箭頭函數(shù)(在ES6中引入)的話(huà),在語(yǔ)法上會(huì)更加優(yōu)雅。

.forEach() 的主要缺點(diǎn)是:

  • 不能在它的循環(huán)體中使用 await。
  • 不能提前退出 .forEach() 循環(huán)。而在 for 循環(huán)中可以使用 break。

中止 .forEach() 的解決方法

如果想要中止 .forEach() 之類(lèi)的循環(huán),有一種解決方法:.some() 還會(huì)循環(huán)遍歷所有數(shù)組元素,并在其回調(diào)返回真值時(shí)停止。

  1. const arr = ['red', 'green', 'blue']; 
  2. arr.some((elem, index) => { 
  3.   if (index >= 2) { 
  4.     return true; // 中止循環(huán) 
  5.   } 
  6.   console.log(elem); 
  7.   //此回調(diào)隱式返回 `undefined`,這 
  8.   //是一個(gè)偽值。 因此,循環(huán)繼續(xù)。 
  9. }); 
  10.  
  11. // Output: 
  12. // 'red' 
  13. // 'green' 

可以說(shuō)這是對(duì) .some() 的濫用,與 for-of 和 break 比起來(lái),要理解這段代碼并不容易。

for-of循環(huán) [ES6]for-of

循環(huán)在 ECMAScript 6 開(kāi)始支持:

  1. const arr = ['a', 'b', 'c']; 
  2. arr.prop = 'property value'
  3.  
  4. for (const elem of arr) { 
  5.   console.log(elem); 
  6. // Output: 
  7. // 'a' 
  8. // 'b' 
  9. // 'c' 

for-of 在循環(huán)遍歷數(shù)組時(shí)非常有效:

  • 用來(lái)遍歷數(shù)組元素。
  • 可以使用 await,如果有需要,可以輕松地遷移到 for-await-of。
  • 甚至可以將 break 和 continue 用于外部作用域。

for-of 和可迭代對(duì)象

for-of 不僅可以遍歷數(shù)組,還可以遍歷可迭代對(duì)象,例如遍歷 Map:

  1. const myMap = new Map() 
  2.   .set(false, 'no') 
  3.   .set(true, 'yes') 
  4. for (const [key, value] of myMap) { 
  5.   console.log(key, value); 
  6.  
  7. // Output: 
  8. // false, 'no' 
  9. // true, 'yes' 

遍歷 myMap 會(huì)生成 [鍵,值] 對(duì),可以通過(guò)對(duì)其進(jìn)行解構(gòu)來(lái)直接訪(fǎng)問(wèn)每一對(duì)數(shù)據(jù)。

for-of 和數(shù)組索引

數(shù)組方法 .entries() 返回一個(gè)可迭代的 [index,value] 對(duì)。如果使用 for-of并使用此方法進(jìn)行解構(gòu),可以很方便地訪(fǎng)問(wèn)數(shù)組索引:

  1. const arr = ['chocolate', 'vanilla', 'strawberry']; 
  2.  
  3. for (const [index, elem] of arr.entries()) { 
  4.   console.log(index, elem); 
  5. // Output: 
  6. // 0, 'chocolate' 
  7. // 1, 'vanilla' 
  8. // 2, 'strawberry' 

總結(jié)

for-of 循環(huán)的的可用性比 for,for-in 和 .forEach() 更好。

通常四種循環(huán)機(jī)制之間的性能差異應(yīng)該是無(wú)關(guān)緊要。如果你要做一些運(yùn)算量很大的事,還是切換到 WebAssembly 更好一些。

 

責(zé)任編輯:趙寧寧 來(lái)源: 前端先鋒
相關(guān)推薦

2019-07-25 10:08:05

JavaScript數(shù)組轉(zhuǎn)換

2010-04-16 10:24:17

Oracle存儲(chǔ)過(guò)程

2021-03-29 12:01:00

遍歷數(shù)組for循環(huán)

2022-09-07 11:52:48

forforEach前端

2009-11-17 15:00:19

PHP遍歷數(shù)組

2016-10-08 21:25:36

Javascript數(shù)組Web

2024-04-25 07:54:46

遍歷數(shù)組PythonFor循環(huán)

2021-06-15 10:01:27

JavaScript數(shù)組遍歷Entries

2021-06-18 10:05:14

JavaScript數(shù)組遍歷

2025-02-10 07:00:00

JavaScript數(shù)組方法前端

2010-11-24 13:11:06

MySQL遍歷數(shù)據(jù)表

2022-04-12 08:46:30

for 循環(huán)遍歷字符串

2022-12-13 12:55:15

Bash循環(huán)

2023-12-11 21:45:52

Javaforeach循環(huán)結(jié)構(gòu)

2020-06-30 10:37:55

JavaScript開(kāi)發(fā)技術(shù)

2024-08-30 08:43:24

JavaScriptforEachfor循環(huán)

2024-03-21 14:27:13

JavaScript數(shù)組

2019-03-04 09:22:52

阿里巴巴foreach Java

2022-11-13 15:33:30

JavaScript數(shù)組開(kāi)發(fā)

2021-11-05 06:57:49

數(shù)組對(duì)象遍歷器
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)