本篇文章給大家分享的是有關(guān)JavaScript中實(shí)現(xiàn)循環(huán)遍歷數(shù)組的方法有哪些,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。

for 循環(huán):
for (let index=0; index < someArray.length; index++) {
const elem = someArray[index];
// ···
}for-in 循環(huán):
for (const key in someArray) {
console.log(key);
}數(shù)組方法 .forEach():
someArray.forEach((elem, index) => {
console.log(elem, index);
});for-of 循環(huán):
for (const elem of someArray) {
console.log(elem);
}for-of 通常是很好選擇。我們會(huì)明白原因。
JavaScript 中的 for 循環(huán)很古老,它在 ECMAScript 1 中就已經(jīng)存在了。for 循環(huán)記錄 arr 每個(gè)元素的索引和值:
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)缺點(diǎn)是什么?
它用途廣泛,但是當(dāng)我們要遍歷數(shù)組時(shí)也很麻煩。
如果我們不想從第一個(gè)數(shù)組元素開始循環(huán)時(shí)它仍然很有用,用其他的循環(huán)機(jī)制很難做到這一點(diǎn)。
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ù)組的好方法:
它訪問(wèn)的是屬性鍵,而不是值。
作為屬性鍵,數(shù)組元素的索引是字符串,而不是數(shù)字。
它訪問(wèn)的是所有可枚舉的屬性鍵(自己的和繼承的),而不僅僅是 Array 元素的那些。
for-in 訪問(wèn)繼承屬性的實(shí)際用途是:遍歷對(duì)象的所有可枚舉屬性。
鑒于 for 和 for-in 都不特別適合在數(shù)組上循環(huán),因此在 ECMAScript 5 中引入了一個(gè)輔助方法: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這種方法確實(shí)很方便:它使我們無(wú)需執(zhí)行大量操作就能夠可訪問(wèn)數(shù)組元素和索引。如果用箭頭函數(shù)(在ES6中引入)的話,在語(yǔ)法上會(huì)更加優(yōu)雅。
.forEach() 的主要缺點(diǎn)是:
不能在它的循環(huán)體中使用 await。
不能提前退出 .forEach() 循環(huán)。而在 for 循環(huán)中可以使用 break。
如果想要中止 .forEach() 之類的循環(huán),有一種解決方法:.some() 還會(huì)循環(huán)遍歷所有數(shù)組元素,并在其回調(diào)返回真值時(shí)停止。
const arr = ['red', 'green', 'blue'];
arr.some((elem, index) => {
if (index >= 2) {
return true; // 中止循環(huán)
}
console.log(elem);
//此回調(diào)隱式返回 `undefined`,這
//是一個(gè)偽值。 因此,循環(huán)繼續(xù)。
});
// Output:
// 'red'
// 'green'可以說(shuō)這是對(duì) .some() 的濫用,與 for-of 和 break 比起來(lái),要理解這段代碼并不容易。
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í)非常有效:
用來(lái)遍歷數(shù)組元素。
可以使用 await
如果有需要,可以輕松地遷移到 for-await-of。
甚至可以將 break 和 continue 用于外部作用域。
for-of 不僅可以遍歷數(shù)組,還可以遍歷可迭代對(duì)象,例如遍歷 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 會(huì)生成 [鍵,值] 對(duì),可以通過(guò)對(duì)其進(jìn)行解構(gòu)來(lái)直接訪問(wèn)每一對(duì)數(shù)據(jù)。
數(shù)組方法 .entries() 返回一個(gè)可迭代的 [index,value] 對(duì)。如果使用 for-of 并使用此方法進(jìn)行解構(gòu),可以很方便地訪問(wèn)數(shù)組索引:
const arr = ['chocolate', 'vanilla', 'strawberry'];
for (const [index, elem] of arr.entries()) {
console.log(index, elem);
}
// Output:
// 0, 'chocolate'
// 1, 'vanilla'
// 2, 'strawberry'以上就是JavaScript中實(shí)現(xiàn)循環(huán)遍歷數(shù)組的方法有哪些,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
標(biāo)題名稱:JavaScript中實(shí)現(xiàn)循環(huán)遍歷數(shù)組的方法有哪些-創(chuàng)新互聯(lián)
標(biāo)題網(wǎng)址:http://www.chinadenli.net/article24/dhddje.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、做網(wǎng)站、軟件開發(fā)、微信公眾號(hào)、手機(jī)網(wǎng)站建設(shè)、移動(dòng)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容