JavaScript數組最多可以存放4294967295個項,如果要添加更多的項,則會發生異常。
Array對象覆蓋了toString()方法和valueOf方法,返回特殊的字串。
var aColors = ["red","green","blue"];
alert(aColors.toString());//輸出red,green,blue
alert(aColors.valueOf());//輸出red,green,blue
toLocaleString()方法的傳回值也是由數組構成的字串。
var aColors = ["red","green","blue"];
alert(aColors.toLocaleString());//輸出red, green, blue大多數情況下toLocaleString()方法的輸出值都與toString()方法的輸出值相同,這裡的輸出值多了兩個空格(在green和blue的前面各有一個),在IE6和FF都試過了,把語言地區和Internet語言喜好設定都設成英文國家,結果還是一樣,不知道為什麼,請高手指點。
join()方法,連接字串,只有一個參數即數組項之間使用的字串。
var aColors = ["red","green","blue"];
alert(aColors.join(","));//輸出red,green,blue
alert(aColors.join("]["));//輸出red][green][blue
alert(aColors.join("-我是連接字串-"));//輸出red-我是連接字串-green-我是連接字串-blue
split()方法,分割字串,和join()方法相反split()是把字串轉換成數組
var sColors = "green";
aColors = sColors.split("");
alert(aColors.toString());//輸出g,r,e,e,n
var sColors = "red-green-blue";
aColors = sColors.split("-");
alert(aColors.toString());//輸出red,green,blue
Array對象具有兩個String類具有的方法,即concat()和slice()方法。
concat()方法,參數將被附加在數組末尾
var aColors = ["red","green","blue"];
var aColors2 = aColors.concat("yellow", "purple");
alert(aColors2.toString());//輸出red,green,blue,yellow, purple
alert(aColors.toString());//輸出red,green,blue
slice()方法,取出數組的一部分
var aColors = ["red","green","blue","yellow", "purple"];
var aColors2 = aColors.slice(1);//截取數組從位置1個開始的所有元素(0為第一個元素)
var aColors3 = aColors.slice(1,4);//截取數組從位置1個開始到位置4之前的元素(不包括位置4)
alert(aColors2.toString());//輸出green,blue,yellow,purple
alert(aColors3.toString());//輸出green,blue,yellow
後進先出(LIFO),push()方法和pop()方法
push()方法用於向數組末尾添加一個或多個項
pop()方法用於刪除數組的最後一項
var stack = new Array;
stack.push("red");
stack.push("green");
stack.push("yellow");
alert(stack.toString());//輸出red,green,yellow
var vItem = stack.pop();
alert(vItem);//輸出yellow
alert(stack.toString());//輸出red,green
先進先出(FIFO),shift()方法和unshift()方法
shift()方法用於刪除數組中的第一項
unshift()方法用於把一個項放在數組的第一個位置,然後把所有的項向後移動一個位置var aColors = ["red","green","yellow"];
var vItem = aColors.shift();
alert(aColors.toString());//輸出red,green,yellow
alert(vItem);//輸出red
aColors.unshift("black");
alert(aColors.toString());//輸出black,green,yellow
通過調用shift()和push()方法模仿隊列,後進後出(LILO)
var queue = ["red","green","yellow"];
queue.push("black");
alert(queue.toString());//輸出red,green,yellow,black
var sNextColor = queue.shift();
alert(sNextColor);//輸出red
alert(queue.toString());//輸出green,yellow,black
為數組排序,reverse()方法和sort()方法
reverse()方法,逆序排列
var aColors = ["red","green","blue","1","2","3"];
aColors.reverse();
alert(aColors.toString());//輸出3,2,1,blue,green,red
sort()方法,升序排列
var aColors = ["red","green","blue","1","2","3"];
aColors.sort();
alert(aColors.toString());//輸出1,2,3,blue,green,red
var aColors = ["15","2","3"];
aColors.sort();
alert(aColors.toString());//輸出15,2,3
這裡15排在了2和3的前面,這是因為sort()方法進行排列以前先調用toString()方法將所有值轉換成字串,然後根據字元代碼比較數組項,1小於2所以15排在2前面。。解決方案如下:var aColors = ["15","2","3"];
aColors.sort();
alert(aColors.toString());//輸出15,2,3
var aColors = ["15","2","3"];
aColors.sort(compareIntegers);
alert(aColors.toString());//輸出2,3,15
function compareIntegers(vNum1,vNum2){
var vNum1 = parseInt(vNum1);
var vNum2 = parseInt(vNum2);
if (vNum1 < vNum2){
return -1;
}else if (vNum1 > vNum2){
return 1;
}else{
return 0;
}
}
sort()方法可以僅可以接受一個參數,即比較函數,這裡的比較函數為compareIntegers,需要自己定義。
如需要降序排列只需要先調用一次sort()方法升序排列,再調用reverse()方法把順序反過來就可以了^_^
splice()方法
arr.splice(0,2)//刪除數組中的前兩項
arr.splice(2,0,"red","green")//在位置2處插入red和green
arr.splice(2,1,"red","green")//將刪除位元置2處的項,並插入red和green
var arr = ["1","2","3","4","5","6"];
arr.splice(0,2);
alert(arr.toString());//輸出3,4,5,6
var arr = ["1","2","3","4","5","6"];
arr.splice(2,0,"red","green");
alert(arr.toString());//輸出1,2,red,green,3,4,5,6
var arr = ["1","2","3","4","5","6"];
arr.splice(2,1,"red","green");
alert(arr.toString());//輸出1,2,red,green,4,5,6
寫的有點慢,跟不上看書的速度...