javascript学习笔记(五)Array类型
数组的创建
第一种:
1 var colors = new Array();
2 var colors = new Array(20);//创建包含20项的数组
3 var colors = new Array("Greg");//创建包含1项,即字符串"Greg"的数组
4 var colors = new Array("red","blue","green"); //创建包含3项第二种:
1 var colors = ["red","blue","green"];
2 var colors = [];//创建一个空数组注意:数组的索引是从0开始的
1. length属性
length属性中保存数组的项数,如:
1 var colors = ["red","blue","green"];
2 alert(colors.length); //3 length属性不是只读的,可以利用length属性在数组的末尾移除项,或者添加新的项,如:
1 var colors = ["red","blue","green"];
2 colors.length = 2;
3 alert(colors); //red,blue
4 colors[colors.length] = "black";
5 alert(colors); //red,blue,black2.join()方法,连接数组中的项
1 var colors = ["red","blue","green"];
2 alert(colors.join(",")); //red,blue,green
3 alert(colors.join("||")); //red||blue||green3.数组的栈方法:push()和pop()
push()方法 可以接受任意数量的参数把它们逐个添加的数组的末尾,并返回修改后数组的长度
pop()方法 从数组末尾移除最后一项,减少数组的length值,返回移除的项
1 var colors = new Arrary(); //创建一个数组
2 var count = colors.push("red","green"); //推入两项到数组末尾
3 alert(count); //2
4 count = colors.push("black"); //推入一项到数组末尾
5 alert(count); //3
6 var item = colors.pop(); //移除最后一项并返回该值
7 alert(item); //"black"
8 alert(count); //2
4.数组的队列方法:push()和shift()、unshift()
push()方法同上
shift()方法 移除数组中的第一项并返回该项,数组长度减1
unshift()方法 在数组前端添加任意项,并返回新数组的长度
var colors = new Arrary(); //创建一个数组
var count = colors.push("red","green"); //推入两项到数组末尾
alert(count); //2
count = colors.push("black"); //推入一项到数组末尾
alert(count); //3
var item = colors.shift(); //移除第一项并返回该值
alert(item); //"red"
alert(colors); //green,black
count = colors.unshift("blue"); //推入一项到数组前端
alert(count); //3
alert(colors); //blue,green,black
5.重排序方法:reverse()和sort()
reverse()方法 反转数组项的顺序 www.2cto.com
sort()方法 默认按字符串大小升序排列数组项,可以接受一个比较大小的函数作为参数
1 var values = [1,2,3,4,5];
2 values.reverse();
3 alert(values); //5,4,3,2,1
1 //升序排序函数
2 function compare(value1,value2) {
3 if (value1 < value2) {
4 return -1; //降序改为1
5 } else if (value1 > value2) {
6 return 1; //降序改为-1
7 } else {
8 return 0;
9 }
10 }
1 //数组升序排列
2 var values = [0,1,5,15,20,10];
3 values.sort(compare);
4 alert(values);//0,1,5,10,15,201 //对于数值型可以用这个函数,升序
2 function compare(value1,value2) {
3 return value2 - value1;
4 }6.数组的一些方法:concat()方法、slice()方法和splice()方法
concat()方法 将参数添加到原数组末尾,返回新的数组,原数组不变
slice()方法 返回数组中的项,一个参数时返回指定位置到数组末尾所有的项,两个参数时返回起始位置和结束位置之间的项(不包括结束位置),原数组不变
splice()方法 向数组中插入,删除,或替换数组中的项,返回删除的项(没有删除时返回空数组),原数组改变
1 //concat()方法
2 var colors = ["red","green","blue"];
3 var colors2 = colors.concat("yellow",["black","brown"]);
4 alert(colors); //red,green,blue
5 alert(colors2); //red,green,blue,yellow,black,brown
1 //slice()方法
2 var colors = ["red","green","blue","yellow","black"];
3 var colors2 = colors.slice(1); //一个参数时返回指定位置到数组末尾所有的项
4 var colors3 = colors.slice(1,4); //两个参数时返回起始位置和结束位置之间的项(不包括结束位置)
5 alert(colors2); //green,blue,yellow,black
6 alert(colors3); //green,,blue,yellow
1 //splice()方法
2 //插入项,插入时指定3个参数:起始位置、0(要删除的项)、要插入的项
3 var colors = ["red","green","blue"];
4 var inserted = colors.splice(1,0,"yellow","orange"); //从位置1开始插入两项
5 alert(colors); //red,yellow,orange,green,blue
6 alert(inserted); //空数组
7
8 //替换项,删除时指定3个参数:起始位置、要删除的项、要插入的任意项
9 var colors = ["red","green","blue"];
10 var replaced = colors.splice(1,1,"black","brown"); //删除一项,插入两项
11 alert(colors); //red,black,browm,blue
12 alert(replaced); //green
摘自 晴天漫步