1.js中的数组的长度可变,并且可以存不同类型的值;方法的参数其实就是一个arguments的数组。
var a= ['123', 1, true];
a[3] = "test";
alert(a.length); //输出4
2.toLocaleString(),toString(),valueOf() 返回的是一串由,分隔的字符串。
如var a= ['123', 1, true];
alert(a.valueOf()); //输出 123,1,true
3,用 join可以用指定的字符分隔,如
如 var a = ['123', 1, true];
alert(a.join('|')); //输出 123|1|true
4.数组可以表现的和栈一样,有 push(),pop() 两个方法,遵循后进先出(LIFO)原则
var a = ['123', 1, true];
a.push('test','best');
alert(a.length); //输出5
var item = a.pop();
alert(item);//输出best
alert(a.length); //输出4
5.数组可以表现的和队列一样,有 push(),shift() 两个方法,遵循先进先出(FIFO)原则
var a = ['123', 1, true];
a.push('test',‘best’);
alert(a.length); //输出5
var item = a.shift();
alert(item);//输出123
alert(a.length); //输出4
6. 反转数组项的顺序
方法一: var values = [1, 2, 3, 4, 5];
alert(values.reverse());//输出 5,4,3,2,1
方法二: function compare(r1, r2) {
return r2 - r1;
}
var values = [1, 2, 3, 4, 5];
alert(values.sort(compare));
7.数组的连接
var colors = ["red", "green", "blue"];
var colors2 = colors.concat("yellow",["black","brown"]);
alert(colors2);//输出red,green,blue,yellow,black,brown
8.数组的截取
var colors = ["red", "green", "blue","yellow","purple"];
alert(colors.slice(1,4));//输出 green,blue,yellow
9.删除,插入,替换
删除: var colors = ["red", "green", "blue"];
var removed = colors.splice(0, 1);
alert(colors);//输出 “green”,"blue"
alert(removed);//输出 red
插入: var colors = ["red", "green", "blue"];
var removed = colors.splice(1,0,"yellow","purple");
alert(colors);//输出red,yellow,purple,green,blue
替换:var colors = ["red", "green", "blue"];
var removed = colors.splice(1,1,"yellow","purple");
alert(colors);//输出red,yellow,purple,blue
10,查找元素在数组中的位置 indexOf(),lastIndexOf()