這篇文章主要介紹了es6中數(shù)組如何用for of遍歷的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇es6中數(shù)組如何用for of遍歷文章都會(huì)有所收獲,下面我們一起來看看吧。
在屏邊等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè) 網(wǎng)站設(shè)計(jì)制作按需制作,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),成都全網(wǎng)營(yíng)銷,外貿(mào)營(yíng)銷網(wǎng)站建設(shè),屏邊網(wǎng)站建設(shè)費(fèi)用合理。
es6中數(shù)組可以用for of遍歷?!癴or...of”語句創(chuàng)建一個(gè)循環(huán)來迭代可迭代的對(duì)象,ES6引入“for...of”循環(huán)用以以替代“for...in”和forEach(),并支持新的迭代協(xié)議;“for...of”語句允許開發(fā)者遍歷Arrays(數(shù)組)、Strings(字符串)、Maps(映射)、 Sets(集合)等可迭代的數(shù)據(jù)結(jié)構(gòu)。
for...of
語句創(chuàng)建一個(gè)循環(huán)來迭代可迭代的對(duì)象。在 ES6 中引入的 for...of
循環(huán),以替代 for...in
和 forEach()
,并支持新的迭代協(xié)議。for...of
允許你遍歷 Arrays(數(shù)組), Strings(字符串), Maps(映射), Sets(集合)等可迭代的數(shù)據(jù)結(jié)構(gòu)等。
for (variable of iterable) {
statement
}
variable:每個(gè)迭代的屬性值被分配給該變量。
iterable:一個(gè)具有可枚舉屬性并且可以迭代的對(duì)象。
我們來探討一些用例。
Arrays(數(shù)組)就是類列表(list-like)對(duì)象。數(shù)組原型上有各種方法,允許對(duì)其進(jìn)行操作,比如修改和遍歷等操作。下面手在一個(gè)數(shù)組上進(jìn)行的 for...of
操作:
// array-example.js
const iterable = ['mini', 'mani', 'mo'];
for (const value of iterable) {
console.log(value);
}
// Output:
// mini
// mani
// mo
其結(jié)果就是打印出 iterable
數(shù)組中的每一個(gè)值。
Map 對(duì)象就是保存 key-value(鍵值) 對(duì)。對(duì)象和原始值可以用作 key(鍵)或 value(值)。Map 對(duì)象根據(jù)其插入方式迭代元素。換句話說, for...of
循環(huán)將為每次迭代返回一個(gè) key-value(鍵值) 數(shù)組。
// map-example.js
const iterable = new Map([['one', 1], ['two', 2]]);
for (const [key, value] of iterable) {
console.log(`Key: ${key} and Value: ${value}`);
}
// Output:
// Key: one and Value: 1
// Key: two and Value: 2
Set(集合) 對(duì)象允許你存儲(chǔ)任何類型的唯一值,這些值可以是原始值或?qū)ο蟆?Set(集合) 對(duì)象只是值的集合。 Set(集合) 元素的迭代基于其插入順序。 Set(集合) 中的值只能發(fā)生一次。如果您創(chuàng)建一個(gè)具有多個(gè)相同元素的 Set(集合) ,那么它仍然被認(rèn)為是單個(gè)元素。
// set-example.js
const iterable = new Set([1, 1, 2, 2, 1]);
for (const value of iterable) {
console.log(value);
}
// Output:
// 1
// 2
盡管我們的 Set(集合) 有多個(gè) 1
和 2
,但輸出的只有 1
和 2
。
字符串用于以文本形式存儲(chǔ)數(shù)據(jù)。
// string-example.js
const iterable = 'javascript';
for (const value of iterable) {
console.log(value);
}
// Output:
// "j"
// "a"
// "v"
// "a"
// "s"
// "c"
// "r"
// "i"
// "p"
// "t"
這里,對(duì)字符串執(zhí)行迭代,并打印出每個(gè)索引上的字符。
把一個(gè)參數(shù)對(duì)象看作是一個(gè)類數(shù)組(array-like)對(duì)象,并且對(duì)應(yīng)于傳遞給函數(shù)的參數(shù)。這是一個(gè)用例:
// arguments-example.js
function args() {
for (const arg of arguments) {
console.log(arg);
}
}
args('a', 'b', 'c');
// Output:
// a
// b
// c
你可能會(huì)想,發(fā)生了什么事?! 如前所述,當(dāng)調(diào)用函數(shù)時(shí),arguments
會(huì)接收傳入 args()
函數(shù)的任何參數(shù)。所以,如果我們傳遞 20 個(gè)參數(shù)給 args()
函數(shù),我們將打印出 20 個(gè)參數(shù)。
生成器是一個(gè)函數(shù),它可以退出函數(shù),稍后重新進(jìn)入函數(shù)。
// generator-example.js
function* generator(){
yield 1;
yield 2;
yield 3;
};
for (const g of generator()) {
console.log(g);
}
// Output:
// 1
// 2
// 3
function*
定義了一個(gè)生成器函數(shù),該函數(shù)返回生成器對(duì)象(Generator object)。更多關(guān)于生成器相關(guān)的信息,可以點(diǎn)擊這里。
JavaScript 提供了四種已知的終止循環(huán)執(zhí)行的方法:break
、continue
、return
和 throw
。讓我們來看一個(gè)例子:
const iterable = ['mini', 'mani', 'mo'];
for (const value of iterable) {
console.log(value);
break;
}
// Output:
// mini
在這個(gè)例子中,我們使用 break
關(guān)鍵字在一次執(zhí)行后終止循環(huán),所以只有 mini
被打印出來。
for...of
循環(huán)僅適用于迭代。 而普通對(duì)象不可迭代。 我們來看一下:
const obj = { fname: 'foo', lname: 'bar' };
for (const value of obj) { // TypeError: obj[Symbol.iterator] is not a function
console.log(value);
}
在這里,我們定義了一個(gè)普通對(duì)象 obj
,并且當(dāng)我們嘗試 for...of
對(duì)其進(jìn)行操作時(shí),會(huì)報(bào)錯(cuò):TypeError: obj[Symbol.iterator] is not a function
。
我們可以通過將類數(shù)組(array-like)對(duì)象轉(zhuǎn)換為數(shù)組來繞過它。該對(duì)象將具有一個(gè) length
屬性,其元素必須可以被索引。我們來看一個(gè)例子:
// object-example.js
const obj = { length: 3, 0: 'foo', 1: 'bar', 2: 'baz' };
const array = Array.from(obj);
for (const value of array) {
console.log(value);
}
// Output:
// foo
// bar
// baz
Array.from()
方法可以讓我通過類數(shù)組(array-like)或可迭代對(duì)象來創(chuàng)建一個(gè)新的 Array(數(shù)組) 實(shí)例。
for...in
循環(huán)將遍歷對(duì)象的所有可枚舉屬性。
//for-in-example.js
Array.prototype.newArr = () => {};
Array.prototype.anotherNewArr = () => {};
const array = ['foo', 'bar', 'baz'];
for (const value in array) {
console.log(value);
}
// Outcome:
// 0
// 1
// 2
// newArr
// anotherNewArr
for...in
不僅枚舉上面的數(shù)組聲明,它還從構(gòu)造函數(shù)的原型中查找繼承的非枚舉屬性,在這個(gè)例子中,newArr
和 anotherNewArr
也會(huì)打印出來。
for...of
更多用于特定于集合(如數(shù)組和對(duì)象),但不包括所有對(duì)象。
注意:任何具有 Symbol.iterator
屬性的元素都是可迭代的。
Array.prototype.newArr = () => {};
const array = ['foo', 'bar', 'baz'];
for (const value of array) {
console.log(value);
}
// Outcome:
// foo
// bar
// baz
for...in
不考慮構(gòu)造函數(shù)原型的不可枚舉屬性。它只需要查找可枚舉屬性并將其打印出來。
關(guān)于“es6中數(shù)組如何用for of遍歷”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“es6中數(shù)組如何用for of遍歷”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
新聞標(biāo)題:es6中數(shù)組如何用forof遍歷
網(wǎng)頁網(wǎng)址:http://aaarwkj.com/article44/jjhohe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、、App設(shè)計(jì)、品牌網(wǎng)站制作、靜態(tài)網(wǎng)站、網(wǎng)站改版
聲明:本網(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í)需注明來源: 創(chuàng)新互聯(lián)