久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放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. 站長(zhǎng)資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      總結(jié)javascript中遍歷數(shù)組的幾種方法

      本篇文章給大家總結(jié)了一些javascript遍歷數(shù)組的幾種方法。有一定的參考價(jià)值,有需要的朋友可以參考一下,希望對(duì)大家有所幫助。

      總結(jié)javascript中遍歷數(shù)組的幾種方法

      有幾種方法可以遍歷數(shù)組,下面將逐個(gè)羅列!

      while循環(huán)

      let index = 0; const array = [1, 2, 3, 4, 5];  while (index < array.length) {     console.log(array[index]);     index++; }

      總結(jié)javascript中遍歷數(shù)組的幾種方法

      for循環(huán)

      const array = [1,2,3,4,5]; for(let index=0;index<array.length;index++){     console.log(array[index]); } for(let index in array){     console.log(array[index]); }

      總結(jié)javascript中遍歷數(shù)組的幾種方法

      forEach

      const array=[1,2,3,4,5]; array.forEach(function(current_value,index,array){     console.log(`At index ${index} in array ${array} the value is ${current_value}`) })

      總結(jié)javascript中遍歷數(shù)組的幾種方法

      map

      最后一個(gè)構(gòu)造很有用,但是不會(huì)返回新數(shù)組,這對(duì)于你的特定情況可能是不希望的。map通過(guò)對(duì)每個(gè)元素應(yīng)用一個(gè)函數(shù)然后返回新數(shù)組來(lái)解決此問(wèn)題。

      const array = [1,2,3,4,5]; const square = x =>Math.pow(x,2); const squares = array.map(square); console.log(`${array}`); console.log(`${squares}`)

      總結(jié)javascript中遍歷數(shù)組的幾種方法

      reduce

      reduce()方法對(duì)累加器和數(shù)組中的每個(gè)元素(從左到右)應(yīng)用一個(gè)函數(shù),以將其減小為單個(gè)值

      const array = [1,2,3,4,5]; const sum = (x,y) => x + y;  const array_sum = array.reduce(sum,0); console.log(`the sum of arrray:${array} is ${array_sum}`);

      總結(jié)javascript中遍歷數(shù)組的幾種方法

      filter

      根據(jù)布爾函數(shù)過(guò)濾篩選數(shù)組中的元素

      const array = [1,2,3,4,5]; const even = x => x%2 === 0; const even_array = array.filter(even); console.log(`even numbers in array ${array} : ${even_array}`);

      總結(jié)javascript中遍歷數(shù)組的幾種方法

      every

      得到了一個(gè)數(shù)組,想測(cè)試每個(gè)元素是否滿(mǎn)足給定條件

      const array = [1,2,3,4,5,8]; const under_six = x => x<6; if(array.every(under_six)){     console.log(`every elemnet in the array is less than 6`); } else{     console.log(`at least one element in the array was bigger than 6`); }

      總結(jié)javascript中遍歷數(shù)組的幾種方法

      some

      測(cè)試是否至少有一個(gè)元素與布爾函數(shù)匹配

      const array = [2,4,5,6,8]; const over_five = x => x>5;  if(array.some(over_five)){     console.log(`at least one element bigger than 5 was found`); } else{     console.log(`no element bigger than 5 was found`); }

      總結(jié)javascript中遍歷數(shù)組的幾種方法

      到此就結(jié)束啦,

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