JavaScript筆記6-數組新方法

來源:互聯網
上載者:User

標籤:儲存   code   並且   indexof   log   script   function   指定   索引   

七.ECMAScript5關於數組的新方法
1.forEach():遍曆數組,並為每個元素調用傳入的函數;
     舉例:
   

  1  var a = [1,2,3];  2      var sum = 0;  3      //傳一個參數  4     a.forEach(function(v){  5          sum += v;  6      });  7      console.log(sum);//6  8      //傳三個參數(元素值,索引,數組本身)  9      a.forEach(function(v,i,a){ 10          a[i]=v+1;//為數組的各元素自加1 11      }) 12      console.log(a);//[2,3,4]

2.map():將調用該函數的數組的每個元素都傳入指定函數,並返回運算後的新數組;
注意:該函數不修改原數組.
舉例:

  1 var a = [1,2,3];  2 var b = a.map(function(x){  3      return x*x;  4  })  5  console.log(b);//[1,4,9]


3.filter():將調用該函數的數組的每個元素傳入指定函數進行判定,若判定為true,則返回.最終返回一個符合指定條件(函數)的元素的集合(該集合為原數組的子集)
注:該函數會跳過稀疏數組中缺少的元素,總是返回稠密數組,利用此特性可以壓縮稀疏數組.
     舉例1:
   

  1  var a = [1,2,3];  2      var c = a.filter(function(x){  3          return x<3;  4      })  5      console.log(c);

     舉例2:
   
  1  var a = [ ,1,null,3,4, ,];  2      console.log(a.length);//6  3      var dense = a.filter(function(){  4          return true;//過濾掉undefined  5      });  6      console.log(dense);//[1,null,3,4]  7      console.log(a);//Array(6) […, 1, null, 3, 4]未改變原有數組  8     var c = a.filter(function(x){  9      return x!=undefined && x != null; 10      }); 11      console.log(c);//Array(3) [1, 3, 4]

4.every():遍曆調用該方法數組的每個元素,判定是否符合指定條件(函數),全部符合則返回true;注意空數組調用時返回true;
some():遍曆調用該方法數組的每個元素,判定是否符合指定條件(函數),只要有一個符合則返回true;注意空數組調用時返回false;
     舉例1:
   

  1  var a = [1,2,3,4,5];  2      var b = a.every(function(x){  3         return x >0;//判定是否所有元素均大於0  4      });  5      console.log(b);//true  6      var c = a.some(function(x){  7          return x < 4; //判定是否有元素小於4  8      });  9      console.log(c);//true

     舉例2:空數組調用
    
  1 var d = [];  2      console.log(d.every(function(x){  3          return x;  4      }));//true  5      console.log(d.some(function(x){  6          return x;  7      }));//false

5.reduce()和reduceRight()
使用指定的函數對數組元素進行組合,產生單個值.指定函數需要兩個值(第一個是執行化簡操作的函數;第二個(可選)是傳遞給函數的初始值)
注意:1)無第二個參數時,函數會調用數組的第一個元素作為初始值,若數組為空白數組則報"類型錯誤"異常;
2)若數組只有一個元素,並且未提供第二個參數,則不會調用函數,只是返回該唯一元素;
舉例1:

  1 var a = [1,2,3,4,5];  2  //求和  3 var sum = a.reduce(function(x,y){  4      return x+y;  5  },0);  6  console.log(sum);//15  7  //求積  8 var mul = a.reduce(function(x,y){  9      return x*y; 10  },1) 11  console.log(mul);//120 12  //求最大值 13 var max = a.reduce(function(x,y){ 14      return x>y?x:y; 15  },0) 16  console.log(max);//5

舉例2:冪運算
  1 var a = [2,3];  2 var big = a.reduceRight(function(x,y){  3      return Math.pow(y,x);  4  });  5  console.log(big);//2^3=8  6   7 var big1 = a.reduceRight(function(x,y){  8      return Math.pow(x,y);  9  }); 10  console.log(big1);//3^2=9 11 


6.indexOf()和lastIndexOf()
返回給定索引值的元素,lastIndexOf()從右往左搜尋;若沒有找到,則返回-1;
注意:該函數可接收兩個參數,第一個參數為需要搜尋的索引;第二個為開始搜尋的位置(可選);兩個參數都不為函數;
舉例:定義一個函數,尋找數組中所有出現的x,並返回包含所有索引的數組

  1 function findall(a,x){  2      var results = [];    //定義空數組,用來接收x的索引值  3     len = a.length;    //搜尋的最大長度  4     pos = 0;    //開始搜尋的位置  5     while(pos<len){  6          pos = a.indexOf(x,pos); //從pos開始搜尋x  7          if(pos === -1){  8              break;    //找不到就返回  9         } 10          results.push(pos);//儲存索引值 11         pos = pos+1; 12      } 13      return results; 14  } 15 測試 16 var a = [1,2,3,2,4]; 17  console.log(findall(a,2));//Array(2) [1, 3]

八.判定是否為數組
Array.isArray():判定是否為數群組類型,是則返回true;

九.字串可以作為唯讀數組
可以使用方括弧([])訪問單個字元;
也可以作用charAt()方法
舉例:

  1 var s = test;  2  s.charAt(0);//"t"  3  s[0];//"t"

十:需求:將完全由數字組成的字串轉為數字類型,如"1345"轉為1345
法一:

  1 ‘use strict‘;  2  function string2int(s){  3      //將s轉為字串數組  4     var str = s.split(‘‘);  5      //調用map()將字串數組轉為數字數組  6     var arr = str.map(function(x){  7          //字元-0即可變為數字  8         return x-0;  9      }) 10      //調用reduce(),將數字數組轉為int類型 11     var res = arr.reduce(function(x,y){ 12         return x*10+y; 13      }) 14      //返回結果 15     return res; 16  }

法二:
  1 ‘use strict‘;  2 var b = "1345";  3  //split():將字串轉為字元類型的數組  4 var c = b.split(‘‘);  5  //與0做減法運算,將字元類型轉為數字類型  6 var d = [];  7  for(var i= 0;i<c.length;i++){  8      d[i]=c[i]-0;  9  } 10  //轉為整型 11 var e = d.reduce(function(x,y){ 12      return x*10+y; 13  }) 14  console.log(e);

JavaScript筆記6-數組新方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.