睡到⑩②點
专注于ASP.NET Ajax
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

写的有点慢,跟不上看书的速度...
posted on 2007-04-14 14:14  睡到⑩②點  阅读(1072)  评论(1编辑  收藏  举报