溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

es6中數組如何用for of遍歷

發布時間:2022-10-24 17:39:21 來源:億速云 閱讀:143 作者:iii 欄目:web開發

這篇文章主要介紹了es6中數組如何用for of遍歷的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇es6中數組如何用for of遍歷文章都會有所收獲,下面我們一起來看看吧。

es6中數組可以用for of遍歷?!癴or...of”語句創建一個循環來迭代可迭代的對象,ES6引入“for...of”循環用以以替代“for...in”和forEach(),并支持新的迭代協議;“for...of”語句允許開發者遍歷Arrays(數組)、Strings(字符串)、Maps(映射)、 Sets(集合)等可迭代的數據結構。

什么是 for…of 循環

for...of 語句創建一個循環來迭代可迭代的對象。在 ES6 中引入的 for...of 循環,以替代 for...inforEach() ,并支持新的迭代協議。for...of 允許你遍歷 Arrays(數組), Strings(字符串), Maps(映射), Sets(集合)等可迭代的數據結構等。

語法

for (variable of iterable) {
    statement
}

  • variable:每個迭代的屬性值被分配給該變量。

  • iterable:一個具有可枚舉屬性并且可以迭代的對象。

用例

我們來探討一些用例。

Arrays(數組)

Arrays(數組)就是類列表(list-like)對象。數組原型上有各種方法,允許對其進行操作,比如修改和遍歷等操作。下面手在一個數組上進行的 for...of 操作:

// array-example.js
const iterable = ['mini', 'mani', 'mo'];
 
for (const value of iterable) {
  console.log(value);
}
 
// Output:
// mini
// mani
// mo

其結果就是打印出 iterable 數組中的每一個值。

Maps(映射)

Map 對象就是保存 key-value(鍵值) 對。對象和原始值可以用作 key(鍵)或 value(值)。Map 對象根據其插入方式迭代元素。換句話說, for...of 循環將為每次迭代返回一個 key-value(鍵值) 數組。

// 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(集合)

Set(集合) 對象允許你存儲任何類型的唯一值,這些值可以是原始值或對象。 Set(集合) 對象只是值的集合。 Set(集合) 元素的迭代基于其插入順序。 Set(集合) 中的值只能發生一次。如果您創建一個具有多個相同元素的 Set(集合) ,那么它仍然被認為是單個元素。

// set-example.js
const iterable = new Set([1, 1, 2, 2, 1]);
 
for (const value of iterable) {
  console.log(value);
}
// Output:
// 1
// 2

盡管我們的 Set(集合) 有多個 12 ,但輸出的只有 12 。

String(字符串)

字符串用于以文本形式存儲數據。

// 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"

這里,對字符串執行迭代,并打印出每個索引上的字符。

Arguments Object(參數對象)

把一個參數對象看作是一個類數組(array-like)對象,并且對應于傳遞給函數的參數。這是一個用例:

// arguments-example.js
function args() {
  for (const arg of arguments) {
    console.log(arg);
  }
}
 
args('a', 'b', 'c');
// Output:
// a
// b
// c

你可能會想,發生了什么事?! 如前所述,當調用函數時,arguments 會接收傳入 args() 函數的任何參數。所以,如果我們傳遞 20 個參數給 args() 函數,我們將打印出 20 個參數。

Generators(生成器)

生成器是一個函數,它可以退出函數,稍后重新進入函數。

// generator-example.js
function* generator(){ 
  yield 1; 
  yield 2; 
  yield 3; 
}; 
 
for (const g of generator()) { 
  console.log(g); 
}
 
// Output:
// 1
// 2
// 3

function* 定義了一個生成器函數,該函數返回生成器對象(Generator object)。更多關于生成器相關的信息,可以點擊這里。

退出迭代

JavaScript 提供了四種已知的終止循環執行的方法:break、continue、returnthrow。讓我們來看一個例子:

const iterable = ['mini', 'mani', 'mo'];
 
for (const value of iterable) {
  console.log(value);
  break;
}
 
// Output:
// mini

在這個例子中,我們使用 break 關鍵字在一次執行后終止循環,所以只有 mini 被打印出來。

普通對象不可迭代

for...of 循環僅適用于迭代。 而普通對象不可迭代。 我們來看一下:

const obj = { fname: 'foo', lname: 'bar' };
 
for (const value of obj) { // TypeError: obj[Symbol.iterator] is not a function
    console.log(value);
}

在這里,我們定義了一個普通對象 obj ,并且當我們嘗試 for...of 對其進行操作時,會報錯:TypeError: obj[Symbol.iterator] is not a function。

我們可以通過將類數組(array-like)對象轉換為數組來繞過它。該對象將具有一個 length 屬性,其元素必須可以被索引。我們來看一個例子:

// 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() 方法可以讓我通過類數組(array-like)或可迭代對象來創建一個新的 Array(數組) 實例。

For…of vs For…in

for...in 循環將遍歷對象的所有可枚舉屬性。

//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 不僅枚舉上面的數組聲明,它還從構造函數的原型中查找繼承的非枚舉屬性,在這個例子中,newArranotherNewArr 也會打印出來。

for...of 更多用于特定于集合(如數組和對象),但不包括所有對象。

注意:任何具有 Symbol.iterator 屬性的元素都是可迭代的。

Array.prototype.newArr = () => {};
const array = ['foo', 'bar', 'baz'];
 
for (const value of array) { 
  console.log(value);
}
// Outcome:
// foo
// bar
// baz

for...in 不考慮構造函數原型的不可枚舉屬性。它只需要查找可枚舉屬性并將其打印出來。

關于“es6中數組如何用for of遍歷”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“es6中數組如何用for of遍歷”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女