JavaScript進階程式設計 讀書筆記之九 本機物件Array

來源:互聯網
上載者:User

建立Array對象 複製代碼 代碼如下://one
var aValues=new Array();

//two
var aValues=new Array(20);

//three
var aColors=new Array();
aColors[0]="red";
aColors[1]="green";
aColors[2]="blue";

//four
var aColors=new Array("red","green","blue");

//five
var aColors=["red","green","blue"];

join && split
join:連接字串 複製代碼 代碼如下:var aColors=["red","green","blue"];
alert(aColors.join(","));//outputs "red,green,blue"
alert(aColors.join("-spring-"));//outputs "red-spring-green-spring-blue"
alert(aColors.join("]["));//outputs "red][green][blue"

split:分拆字串 複製代碼 代碼如下:var sColors="red,green,blue";
var aColors=sColors.split(",");//outputs ["red", "green", "blue"]
var redColors=aColors[0].split("");//outputs ["r", "e", "d"]

concat && slice
concat:追加數組 複製代碼 代碼如下:var aColors=["red","green","blue"];
var aColors2=aColors.concat("yellow","purple");
alert(aColors);//outputs ["red", "green", "blue"]
alert(aColors2);//outputs ["red", "green", "blue", "yellow", "purple"]

slice:返回具有特定項的新數組 複製代碼 代碼如下:var aColors=["red","green","blue","yellow","purple"];
var aColors2=aColors.slice(1);//outputs ["green","blue","yellow","purple"]
var aColors3=aColors.slice(1,4);//outputs ["green","blue","yellow"]

push && pop
跟棧一樣,Array提供了push和pop方法,push方法用於在Array結尾添加一個或多個項,pop用於刪除最後一個數組項,返回它作為函數值 複製代碼 代碼如下:var stack=new Array;
stack.push("red");
stack.push("green");
stack.push("blue");
alert(stack);//outputs ["red","green","blue"]
var vItem=stack.pop();
alert(vItem);//outputs ["blue"]
alert(stack);//otputs ["red","green"]

shift && unshift
shift:刪除數組中第一項,將其作為函數傳回值,unshift:把一個項放在數組的第一個位置,然後把餘下的項向下移動一個位置 複製代碼 代碼如下:var aColors=["red","green","blue"];
var vItem=aColors.shift();
alert(aColors);//outputs ["green","blue"]
alert(vItem);//outputs ["red"]
aColors.unshift("black");
alert(aColors);//outputs ["black","green","blue"]

reverse && sort
reverse:顛倒數組項的順序,sort:按數組項的值升序排列(首先要調用toString()方法,將所有值轉換成字串) 複製代碼 代碼如下:var aColors=["blue","green","red"];
aColors.reverse();
alert(aColors);//outputs ["red","green","blue"]
aColors.sort();
alert(aColors);//outputs ["blue","green","red"]

注意: 複製代碼 代碼如下:var aColors=[3,32,2,5];
aColors.sort();
alert(aColors);//outputs [2,3,32,5]

這是因為數字被轉換成字串,然後按字元代碼進行比較的。

splice
splice:把資料項目插入數組的中部

1、用作刪除:只要聲明兩個參數,第一個參數為要刪除的第一個項的位置,第二個參數為刪除項的個數 複製代碼 代碼如下:var aColors=["red","green","blue","yellow"];
aColors.splice(0,2);
alert(aColors);//outputs ["blue", "yellow"]

2、用作插入:聲明三個或以上參數(第二個參數為0)就可以把資料插入指定位置,第一個參數為地始位置,第二個參數為0,第三個及以上參數為插入項 複製代碼 代碼如下:var aColors=["red","green","blue","yellow"];
aColors.splice(2,0,"black","white");
alert(aColors);//outputs ["red","green","black","white","blue", "yellow"]

3、用作刪除並插入:聲明三個或以上參數(第二個參數為不0)就可以把資料插入指定位置,第一個參數為地始位置,第二個參數為要刪除的項的個數,第三個及以上參數為插入項 複製代碼 代碼如下:var aColors=["red","green","blue","yellow"];
aColors.splice(2,1,"black","white");
alert(aColors);//outputs ["red","green","black","white", "yellow"]

相關文章

聯繫我們

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