js数组(一)
一、创建数组两种方法:
1Array构造
var colors = new Array();
2、字面量表示
什么事字面量,如
var colors = ["red","green",""];
数组项,方括号组成,多个数组项用逗号隔开;
red是数组项
3、
var colors = [,,,]; alert(colors.length);//谷歌是3,IE8以下是4
var names = [];//创建一个空数组
var colors = ["red", "green"];//创建一个包含三项的字符串
4、修改数组
var colors = ["red","green",""]; colors[1] = 'black'; var name = colors[1] alert(name);
5、利用length属性添加末尾项的数组
var colors = ["red", "green"];//创建一个包含三项的字符串 colors[colors.length] = 'black'; alert(colors.length);//3 alert(colors[2]);//black
二、检测数组
1、确定某个对象是不是数组,使用instanceof
var value = ["hello"]; if(value instanceof Array){ alert('yes');//输出YES } else{ alert('no'); }
2、Array.isArray(),IE9以上支持
var value = ["hello"]; if(Array.isArray(value)){ alert('yes'); } else{ alert('no'); }
三、转换方法
1、
四、栈方法
pop(),push()后进先出
1、push()方法
var colors = new Array(); var count = colors.push("red","yellow");//逐个添加数组末尾 alert(count);//2 alert(colors);//red,yellow
var colors = new Array(); var count = colors.push("red","yellow");//逐个添加数组末尾 count = colors.push("black"); alert(count);//3 alert(colors);//red,yellow,black
2、pop()方法
var colors = new Array(); var count = colors.push("red","yellow");//逐个添加数组末尾 count = colors.push("black"); var item = colors.pop();//移除数组末尾项 alert(item);//black alert(colors);//red,yellow
五、队列方法
1先进后出
shift().获取数组第一项,使数组的length-1,配合push()使用
var colors = new Array(); var count = colors.push("red","yellow");//逐个添加数组末尾 count = colors.push("black"); var item = colors.shift(); // 获取第一项,red alert(item);//red alert(colors);//yellow,black
2、unshift()
var colors = new Array(); var count = colors.unshift("red","yellow");//逐个添加数组末尾 alert(colors);//red,yellow count = colors.unshift("black"); alert(colors);//black,red,yellow var item = colors.pop(); // 获取第一项,red alert(item);//yellow alert(colors);//black,red
六、重排序方法
1、reverse(),sort()
var value = [1,2,3,4,5]; value.sort(); alert(value);//1,2,3,4,5
2
var value = [1,2,3,4,5]; value.reverse(); alert(value);//5,4,3,2,1
七、操作方法
1、concat()
2、slice()
var colors = ['red','green','blue','purple','white']; var removed = colors.slice(1); //从位置1开始复制 var colors1 = colors.slice(1,4); //从位置1开始复制,到位置3结束 alert(removed); //green,blue,purple,white alert(colors); //red,green,blue,purple,white*/ alert(colors1); //red,green,purple*/
3、spliace
删除
var colors = ['red','green','blue']; var removed = colors.splice(0,1); //删除第0项 alert(removed); //red alert(colors); //green,blue
插入
var colors = ['red','green','blue']; var removed = colors.splice(1,0,'yellow','orange'); // 从位置1开始插入 alert(colors); // red,yellow,orange,green,blue alert(removed); //空数组
替换
var colors = ['red','green','blue']; var removed = colors.splice(0,1); //删除第1项.colors为green,blue //alert(removed); //red //alert(colors); //green,blue removed = colors.splice(1,0,'yellow','orange'); // 从位置1开始插入 //alert(colors); // green,yellow,orange,blue //alert(removed); //空数组,因为删除0个数组 removed = colors.splice(1,1,'red','purple');//从位置1开始,删除1项,red,purple alert(colors);//green,red,purple,orange,blue alert(removed);//yellow,因为删除yellow这个项