日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長提供免費收錄網(wǎng)站服務(wù),提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

在 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)文章!

分享到:
標簽:javascript 如何將 數(shù)組 轉(zhuǎn)換為 迭代
用戶無頭像

網(wǎng)友整理

注冊時間:

網(wǎng)站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨大挑戰(zhàn)2018-06-03

數(shù)獨一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運動步數(shù)有氧達人2018-06-03

記錄運動步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績評定2018-06-03

通用課目體育訓(xùn)練成績評定