<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      js數組遍歷方法總結

      數組遍歷方法

      1.for循環

      使用臨時變量,將長度緩存起來,避免重復獲取數組長度,當數組較大時優化效果才會比較明顯。

      1
      2
      3
      for(j = 0,len=arr.length; j < len; j++) {
          
      }

       

      2.foreach循環

      遍歷數組中的每一項,沒有返回值,對原數組沒有影響,不支持IE

      1
      2
      3
      4
      5
      6
      //1 沒有返回值
      arr.forEach((item,index,array)=>{
          //執行代碼
      })
      //參數:value數組中的當前項, index當前項的索引, array原始數組;
      //數組中有幾項,那么傳遞進去的匿名回調函數就需要執行幾次;

       

      3.map循環

      有返回值,可以return出來

      map的回調函數中支持return返回值;return的是啥,相當于把數組中的這一項變為啥(并不影響原來的數組,只是相當于把原數組克隆一份,把克隆的這一份的數組中的對應項改變了);

      1
      2
      3
      4
      5
      6
      7
      arr.map(function(value,index,array){
       
        //do something
       
        return XXX
       
      })
      1
      2
      3
      4
      5
      6
      var ary = [12,23,24,42,1]; 
      var res = ary.map(function (item,index,ary ) { 
          return item*10; 
      }) 
      console.log(res);//-->[120,230,240,420,10];  原數組拷貝了一份,并進行了修改
      console.log(ary);//-->[12,23,24,42,1];  原數組并未發生變化

       

      4.forof遍歷

      可以正確響應break、continue和return語句

      1
      2
      3
      for (var value of myArray) {
      console.log(value);
      }

        

      5.filter遍歷

      不會改變原始數組,返回新數組

      1
      2
      3
      4
      5
      var arr = [
        { id: 1, text: 'aa', done: true },
        { id: 2, text: 'bb', done: false }
      ]
      console.log(arr.filter(item => item.done))

      轉為ES5

      1
      2
      3
      arr.filter(function (item) {
        return item.done;
      });
      1
      2
      3
      var arr = [73,84,56, 22,100]
      var newArr = arr.filter(item => item>80)   //得到新數組 [84, 100]
      console.log(newArr,arr)

        

      6.every遍歷

      every()是對數組中的每一項運行給定函數,如果該函數對每一項返回true,則返回true。

      1
      2
      3
      4
      5
      var arr = [ 1, 2, 3, 4, 5, 6 ]; 
      console.log( arr.every( function( item, index, array ){ 
              return item > 3; 
          })); 
      false

       

      7.some遍歷

      some()是對數組中每一項運行指定函數,如果該函數對任一項返回true,則返回true。

      1
      2
      3
      4
      5
      6
      var arr = [ 1, 2, 3, 4, 5, 6 ]; 
         
          console.log( arr.some( function( item, index, array ){ 
              return item > 3; 
          })); 
      true

        

      8.reduce

      reduce() 方法接收一個函數作為累加器(accumulator),數組中的每個值(從左到右)開始縮減,最終為一個值。

      1
      var total = [0,1,2,3,4].reduce((a, b)=>a + b); //10

      reduce接受一個函數,函數有四個參數,分別是:上一次的值,當前值,當前值的索引,數組

      1
      2
      3
      [0, 1, 2, 3, 4].reduce(function(previousValue, currentValue, index, array){
       return previousValue + currentValue;
      });

      reduce還有第二個參數,我們可以把這個參數作為第一次調用callback時的第一個參數,上面這個例子因為沒有第二個參數,所以直接從數組的第二項開始,如果我們給了第二個參數為5,那么結果就是這樣的:

      1
      2
      3
      [0, 1, 2, 3, 4].reduce(function(previousValue, currentValue, index, array){
       return previousValue + currentValue;
      },5);

       

      第一次調用的previousValue的值就用傳入的第二個參數代替,

      9.reduceRight

      reduceRight()方法的功能和reduce()功能是一樣的,不同的是reduceRight()從數組的末尾向前將數組中的數組項做累加。

      reduceRight()首次調用回調函數callbackfn時,prevValue 和 curValue 可以是兩個值之一。如果調用 reduceRight() 時提供了 initialValue 參數,則 prevValue 等于 initialValue,curValue 等于數組中的最后一個值。如果沒有提供 initialValue 參數,則 prevValue 等于數組最后一個值, curValue 等于數組中倒數第二個值。

      1
      2
      3
      4
      5
      var arr = [0,1,2,3,4];
       
      arr.reduceRight(function (preValue,curValue,index,array) {
          return preValue + curValue;
      }); // 10

      回調將會被調用四次,每次調用的參數及返回值如下:

      如果提供一個初始值initialValue5:

      1
      2
      3
      4
      5
      var arr = [0,1,2,3,4];
       
      arr.reduceRight(function (preValue,curValue,index,array) {
          return preValue + curValue;
      }, 5); // 15

      回調將會被調用五次,每次調用的參數及返回的值如下:

      同樣的,可以對一個數組求和,也可以使用reduceRight()方法:

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      var arr = [1,2,3,4,5,6];
       
      console.time("ruduceRight");
      Array.prototype.ruduceRightSum = function (){
          for (var i = 0; i < 10000; i++) {
              return  this.reduceRight (function (preValue, curValue) {
                  return preValue + curValue;
              });
          }
      }
      arr.ruduceRightSum();
      console.log('最終的值:' + arr.ruduceSum()); // 21
      console.timeEnd("ruduceRight"); // 5.725ms

      10.find

      find()方法返回數組中符合測試函數條件的第一個元素。否則返回undefined 

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      var stu = [
          {
              name: '張三',
              gender: '男',
              age: 20
          },
          {
              name: '王小毛',
              gender: '男',
              age: 20
          },
          {
              name: '李四',
              gender: '男',
              age: 20
          }
      ]
      1
      2
      3
      4
      5
      6
      7
      function getStu(element){
         return element.name == '李四'
      }
       
      stu.find(getStu)
      //返回結果為
      //{name: "李四", gender: "男", age: 20}

      ES6方法

      1
      stu.find((element) => (element.name == '李四'))

       

      11.findIndex

      對于數組中的每個元素,findIndex 方法都會調用一次回調函數(采用升序索引順序),直到有元素返回 true只要有一個元素返回 true,findIndex 立即返回該返回 true 的元素的索引值。如果數組中沒有任何元素返回 true,則 findIndex 返回 -1。

      findIndex 不會改變數組對象。

      1
      2
      [1,2,3].findIndex(function(x) { x == 2; });
      // Returns an index value of 1.
      1
      2
      [1,2,3].findIndex(x => x == 4);
      // Returns an index value of -1.

      12.keys,values,entries

       ES6 提供三個新的方法 —— entries(),keys()和values() —— 用于遍歷數組。它們都返回一個遍歷器對象,可以用for...of循環進行遍歷,唯一的區別是keys()是對鍵名的遍歷、values()是對鍵值的遍歷,entries()是對鍵值對的遍歷

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      for (let index of ['a''b'].keys()) {
      console.log(index);
      }
      // 0
      // 1
      for (let elem of ['a''b'].values()) {
      console.log(elem);
      }
      // 'a'
      // 'b'
      for (let [index, elem] of ['a''b'].entries()) {
      console.log(index, elem);
      }
      // 0 "a"
      // 1 "b"
      posted on 2019-04-02 18:06  cruoon  閱讀(142)  評論(0)    收藏  舉報
       
       
      主站蜘蛛池模板: 99久久国产露脸国语对白| 麻豆一区二区三区蜜桃免费| 国产精品一区二区色综合| 国产一区二区三区小说| 无码熟妇人妻av影音先锋| 无套内谢少妇一二三四| 在线a亚洲老鸭窝天堂| 人妻少妇久久中文字幕一区二区| 亚洲精品国产无套在线观| 中文字幕成人精品久久不卡| 欧美人与性动交α欧美精品| 人妻影音先锋啪啪av资源| 国产日韩精品中文字幕| 国产乱xxxxx97国语对白| 国产精品99久久不卡| 亚洲鸥美日韩精品久久| 日韩人妻无码精品久久| 蜜桃无码一区二区三区| 久久天天躁狠狠躁夜夜婷| 禹城市| 国产成人精品视频国产| 亚洲男女羞羞无遮挡久久丫| 国内精品久久毛片一区二区| 国产精品色三级在线观看| 丁香五月亚洲综合在线| 日韩一区二区三在线观看| 东京热大乱系列无码| 亚洲欧美自偷自拍视频图片| 国产综合久久久久久鬼色| 免费无码成人AV在线播放不卡| 北票市| 中文字幕乱码中文乱码毛片 | 久久久久人妻一区二区三区| 久久国产精品不只是精品| 国产成人一区二区三区免费 | 天天躁夜夜躁av天天爽| 国产成年女人特黄特色大片免费 | 亚洲av第二区国产精品| 在线免费不卡视频| 国产午精品午夜福利757视频播放| 亚洲一区精品视频在线|