久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      es6中數(shù)組可以用for of遍歷嗎

      es6中數(shù)組可以用for of遍歷?!癴or…of”語句創(chuàng)建一個循環(huán)來迭代可迭代的對象,ES6引入“for…of”循環(huán)用以以替代“for…in”和forEach(),并支持新的迭代協(xié)議;“for…of”語句允許開發(fā)者遍歷Arrays(數(shù)組)、Strings(字符串)、Maps(映射)、 Sets(集合)等可迭代的數(shù)據(jù)結(jié)構(gòu)。

      es6中數(shù)組可以用for of遍歷嗎

      前端(vue)入門到精通課程:進(jìn)入學(xué)習(xí)
      Apipost = Postman + Swagger + Mock + Jmeter 超好用的API調(diào)試工具:點擊使用

      本教程操作環(huán)境:windows7系統(tǒng)、ECMAScript 6版、Dell G3電腦。

      什么是 for…of 循環(huán)

      for...of 語句創(chuàng)建一個循環(huán)來迭代可迭代的對象。在 ES6 中引入的 for...of 循環(huán),以替代 for...inforEach() ,并支持新的迭代協(xié)議。for...of 允許你遍歷 Arrays(數(shù)組), Strings(字符串), Maps(映射), Sets(集合)等可迭代的數(shù)據(jù)結(jié)構(gòu)等。

      語法

      for (variable of iterable) {     statement }
      登錄后復(fù)制

      • variable:每個迭代的屬性值被分配給該變量。
      • iterable:一個具有可枚舉屬性并且可以迭代的對象。

      用例

      我們來探討一些用例。

      Arrays(數(shù)組)

      Arrays(數(shù)組)就是類列表(list-like)對象。數(shù)組原型上有各種方法,允許對其進(jìn)行操作,比如修改和遍歷等操作。下面手在一個數(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
      登錄后復(fù)制

      其結(jié)果就是打印出 iterable 數(shù)組中的每一個值。

      Demo: https://jsbin.com/dimahag/edit?js,console

      Maps(映射)

      Map 對象就是保存 key-value(鍵值) 對。對象和原始值可以用作 key(鍵)或 value(值)。Map 對象根據(jù)其插入方式迭代元素。換句話說, for...of 循環(huán)將為每次迭代返回一個 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
      登錄后復(fù)制

      Demo: https://jsbin.com/lofewiw/edit?js,console

      Set(集合)

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

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

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

      Demo: https://jsbin.com/fajozob/edit?js,console

      String(字符串)

      字符串用于以文本形式存儲數(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"
      登錄后復(fù)制

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

      Demo: https://jsbin.com/rixakeg/edit?js,console

      Arguments Object(參數(shù)對象)

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

      // arguments-example.js function args() {   for (const arg of arguments) {     console.log(arg);   } }   args('a', 'b', 'c'); // Output: // a // b // c
      登錄后復(fù)制

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

      Demo: https://jsbin.com/ciqabov/edit?js,console

      Generators(生成器)

      生成器是一個函數(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
      登錄后復(fù)制

      function* 定義了一個生成器函數(shù),該函數(shù)返回生成器對象(Generator object)。

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號