js進階程式設計--Array

來源:互聯網
上載者:User

標籤:利用   建立   ack   構造   result   orange   cer   filter   程式設計   

var colors = new Array(20); 建立長度值為20的數組

var colors = new Array(‘red‘,‘blue‘,‘green‘);建立包含三個字串的數組;

在使用Array建構函式的時候也可以省略new操作,var colors = Array(3);

Array.isArray()方法,確定某個值是不是數組

一些操作方法:

concat();可以基於當前數組中的所有項建立一個新數組

 

var colors = [‘red‘,‘green‘,‘blue‘];
var colors2 = colors.concat(‘yellow‘,[‘black‘,‘purple‘]);
alert(colors)    // red,green,orange
alert(colors2)    //red,green,blue,yellow,black,purple

slice();可以接受一個或兩個參數,即起始位置和結束位置,返回起始到結束的所有項,但不包括結束為止的項;在只有一個參數的情況下,則返回從該參數指定位置到當前數組末尾的所有項

splice();  增刪改查

三個參數,起始位置,長度,插入項;

迭代方法:

filter();利用指定的函數確定是否在返回的數組中包含某一項

var num= [1,2,3,4,5,4,3,2,1];

var result = num.filter(function(item,index,array){

  return (item > 2);

})

alert(result);

map();

也返回一個數組,和上面類似

forEach();

var num= [1,2,3,4,5,4,3,2,1];

num.forEach(function(item,index,array){

  //執行某些操作

})

reduce();和reduceRight();兩個歸併數組的方法

都會迴圈數組所有的項,然後構建一個最終返回的值,接受4個參數:前一個值,當前值,項的索引和數組對象

 

var num= [1,2,3,4,5];

 

var result = num.reduce(function(prev,cur,index,array){

  return prev + cur;

})

alert(result);    //15

js進階程式設計--Array

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.