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

創(chuàng)新互聯(lián)服務(wù)項目包括雙清網(wǎng)站建設(shè)、雙清網(wǎng)站制作、雙清網(wǎng)頁制作以及雙清網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,雙清網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到雙清省份的部分城市,未來相信會繼續(xù)擴大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
- for 循環(huán):
- for (let index=0; index < someArray.length; index++) {
- const elem = someArray[index];
- // ···
- }
- for (const key in someArray) {
- console.log(key);
- }
- someArray.forEach((elem, index) => {
- console.log(elem, index);
- });
- for (const elem of someArray) {
- console.log(elem);
- }
for-of 通常是最佳選擇。我們會明白原因。
for循環(huán) [ES1]
JavaScript 中的 for 循環(huán)很古老,它在 ECMAScript 1 中就已經(jīng)存在了。for 循環(huán)記錄 arr 每個元素的索引和值:
- 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 循環(huán)的優(yōu)缺點是什么?
- 它用途廣泛,但是當(dāng)我們要遍歷數(shù)組時也很麻煩。
- 如果我們不想從第一個數(shù)組元素開始循環(huán)時它仍然很有用,用其他的循環(huán)機制很難做到這一點。
for-in循環(huán) [ES1]
for-in 循環(huán)與 for 循環(huán)一樣古老,同樣在 ECMAScript 1中就存在了。下面的代碼用 for-in 循環(huán)輸出 arr 的 key:
- const arr = ['a', 'b', 'c'];
- arr.prop = 'property value';
- for (const key in arr) {
- console.log(key);
- }
- // Output:
- // '0'
- // '1'
- // '2'
- // 'prop'
for-in 不是循環(huán)遍歷數(shù)組的好方法:
- 它訪問的是屬性鍵,而不是值。
- 作為屬性鍵,數(shù)組元素的索引是字符串,而不是數(shù)字。
- 它訪問的是所有可枚舉的屬性鍵(自己的和繼承的),而不僅僅是 Array 元素的那些。
for-in 訪問繼承屬性的實際用途是:遍歷對象的所有可枚舉屬性。
數(shù)組方法.forEach()[ES5]
鑒于 for 和 for-in 都不特別適合在數(shù)組上循環(huán),因此在 ECMAScript 5 中引入了一個輔助方法:Array.prototype.forEach():
- const arr = ['a', 'b', 'c'];
- arr.prop = 'property value';
- arr.forEach((elem, index) => {
- console.log(elem, index);
- });
- // Output:
- // 'a', 0
- // 'b', 1
- // 'c', 2
這種方法確實很方便:它使我們無需執(zhí)行大量操作就能夠可訪問數(shù)組元素和索引。如果用箭頭函數(shù)(在ES6中引入)的話,在語法上會更加優(yōu)雅。
.forEach() 的主要缺點是:
- 不能在它的循環(huán)體中使用 await。
- 不能提前退出 .forEach() 循環(huán)。而在 for 循環(huán)中可以使用 break。
中止 .forEach() 的解決方法
如果想要中止 .forEach() 之類的循環(huán),有一種解決方法:.some() 還會循環(huán)遍歷所有數(shù)組元素,并在其回調(diào)返回真值時停止。
- const arr = ['red', 'green', 'blue'];
- arr.some((elem, index) => {
- if (index >= 2) {
- return true; // 中止循環(huán)
- }
- console.log(elem);
- //此回調(diào)隱式返回 `undefined`,這
- //是一個偽值。 因此,循環(huán)繼續(xù)。
- });
- // Output:
- // 'red'
- // 'green'
可以說這是對 .some() 的濫用,與 for-of 和 break 比起來,要理解這段代碼并不容易。
for-of循環(huán) [ES6]for-of
循環(huán)在 ECMAScript 6 開始支持:
- const arr = ['a', 'b', 'c'];
- arr.prop = 'property value';
- for (const elem of arr) {
- console.log(elem);
- }
- // Output:
- // 'a'
- // 'b'
- // 'c'
for-of 在循環(huán)遍歷數(shù)組時非常有效:
- 用來遍歷數(shù)組元素。
- 可以使用 await,如果有需要,可以輕松地遷移到 for-await-of。
- 甚至可以將 break 和 continue 用于外部作用域。
for-of 和可迭代對象
for-of 不僅可以遍歷數(shù)組,還可以遍歷可迭代對象,例如遍歷 Map:
- 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 會生成 [鍵,值] 對,可以通過對其進行解構(gòu)來直接訪問每一對數(shù)據(jù)。
for-of 和數(shù)組索引
數(shù)組方法 .entries() 返回一個可迭代的 [index,value] 對。如果使用 for-of并使用此方法進行解構(gòu),可以很方便地訪問數(shù)組索引:
- const arr = ['chocolate', 'vanilla', 'strawberry'];
- for (const [index, elem] of arr.entries()) {
- console.log(index, elem);
- }
- // Output:
- // 0, 'chocolate'
- // 1, 'vanilla'
- // 2, 'strawberry'
總結(jié)
for-of 循環(huán)的的可用性比 for,for-in 和 .forEach() 更好。
通常四種循環(huán)機制之間的性能差異應(yīng)該是無關(guān)緊要。如果你要做一些運算量很大的事,還是切換到 WebAssembly 更好一些。
本文名稱:JS中循環(huán)遍歷數(shù)組方式總結(jié)
文章位置:http://fisionsoft.com.cn/article/dpsgjho.html


咨詢
建站咨詢
