在 JavaScript 中,迭代器是元素的集合,通過它我們可以在每次迭代中迭代并訪問單個元素。集合、映射或?qū)ο笫?JavaScript 中的迭代器,我們無法像數(shù)組一樣使用索引來訪問迭代器的元素。
所以,我們首先需要將迭代器轉(zhuǎn)換為數(shù)組。在這里,我們將使用不同的方法(例如 array.from() 等)將迭代器轉(zhuǎn)換為數(shù)組。
使用 for-of 循??環(huán)
for-of 循??環(huán)遍歷迭代器謊言集合和映射的每個元素。在 for-of 循??環(huán)中,我們可以訪問元素并將其添加到數(shù)組中,并且可以使用 push() 方法將元素添加到數(shù)組中。
語法
用戶可以按照下面的語法使用for-of循環(huán)將迭代器轉(zhuǎn)換為數(shù)組。
for (let element of iterator) { array.push(element); }
登錄后復(fù)制
在上面的語法中,我們在 for-of 循??環(huán)中訪問迭代器的元素并將其推送到數(shù)組
示例 1
在下面的示例中,我們創(chuàng)建了 test_array 并用一些數(shù)字對其進行了初始化。之后,我們使用 Symbol.iterator() 將數(shù)組轉(zhuǎn)換為迭代器。
接下來,我們使用 for-of 循??環(huán)來迭代迭代器。我們一一訪問迭代器的所有元素并將它們推入數(shù)組。一旦for循環(huán)的所有迭代完成,我們就可以獲得迭代器的完整數(shù)組。
<html> <body> <h2>Using the <i> for loop </i> to transform JavaScript iterator into the array</h2> <div id = "output"> </div> </body> <script> let output = document.getElementById('output'); let test_array = [10, 20, 30, 0, 50, 60, 70]; let iterator = test_array[Symbol.iterator](); let array = []; for (let element of iterator) { array.push(element); output.innerHTML += "Array element is - " + element + "<br>"; } output.innerHTML += "The whole array is - " + array; </script> </html>
登錄后復(fù)制
使用 array.from() 方法
Array.from() 方法從迭代器創(chuàng)建一個數(shù)組。我們需要將迭代器對象作為 array.from() 方法的參數(shù)傳遞。將迭代器轉(zhuǎn)換為數(shù)組后,它返回一個數(shù)組。
語法
用戶可以按照下面的語法使用 array.from() 方法將迭代器轉(zhuǎn)換為數(shù)組。
let array = Array.from(test_set);
登錄后復(fù)制
在上面的語法中,test_set是一個要轉(zhuǎn)換為數(shù)組的迭代器。
參數(shù)
test_set – 它是一個要轉(zhuǎn)換為數(shù)組的迭代器。
示例 2
在下面的示例中,我們使用各種元素創(chuàng)建了集合。之后,我們使用 array.from() 方法將集合轉(zhuǎn)換為數(shù)組。在輸出中,用戶可以看到從 array.from() 方法返回的數(shù)組。
<html> <body> <h2>Using the <i> Array.from() method </i> to transform JavaScript iterator into the array.</h2> <div id = "output"> </div> </body> <script> let output = document.getElementById('output'); let test_set = new Set(["Hello", "Hello", "Hi", 10, 10, 20, 30, 40, 40, true, false, true, true]); let array = Array.from(test_set); output.innerHTML += "The array from the test_set is " + array; </script> </html>
登錄后復(fù)制
使用擴展運算符
擴展運算符還允許我們像 array.from() 方法一樣將迭代器轉(zhuǎn)換為數(shù)組。它將迭代器的所有元素復(fù)制到新數(shù)組中。另外,我們可以用它來克隆數(shù)組。
語法
用戶可以按照下面的語法使用擴展運算符將迭代器轉(zhuǎn)換為數(shù)組
let array = [...test_map];
登錄后復(fù)制
在上面的語法中,test_map是一個迭代器。
示例 3
在下面的示例中,我們創(chuàng)建了具有唯一鍵和值的映射。我們可以使用鍵從映射中訪問特定值。
我們已使用擴展運算符將 test_map 轉(zhuǎn)換為數(shù)組。在輸出中,用戶可以看到映射的每個鍵和值都已添加到數(shù)組中
<html> <body> <h2>Using the <i> Spread operator </i> to transform JavaScript iterator into the array.</h2> <div id = "output"> </div> </body> <script> let output = document.getElementById('output'); var test_map = new Map([["first", true], ["second", false], ["third", false], ["fourth", true]]); let array = [...test_map]; output.innerHTML += "The array from the test_map is " + array; </script> </html>
登錄后復(fù)制
示例 4
在此示例中,我們將集合迭代器轉(zhuǎn)換為數(shù)組。 new Set() 構(gòu)造函數(shù)用于從數(shù)字、布爾值和字符串元素創(chuàng)建集合。
之后,我們使用展開運算符將集合迭代器轉(zhuǎn)換為數(shù)組。
<html> <body> <h2>Using the <i> Spread operator </i> to transform JavaScript iterator into the array.</h2> <div id="output"> </div> </body> <script> let output = document.getElementById('output'); let set = new Set([30, 40, "TypeScript", "JavaScript"]) let array = [...set] output.innerHTML += "The array from the object is " + array; </script> </html>
登錄后復(fù)制
在本教程中,我們了解了將迭代器轉(zhuǎn)換為數(shù)組的三種不同方法。最好的方法是使用展開運算符,因為它還提供了另一種功能,例如克隆數(shù)組。
以上就是如何將 JavaScript 迭代器轉(zhuǎn)換為數(shù)組?的詳細內(nèi)容,更多請關(guān)注www.92cms.cn其它相關(guān)文章!