js数组有两种定义方式:new Array()和[]。
var list = new Array();
var list = [];
现在来看些基本功能
var list = new Array(3,5);
list[2] = 7;
list[3] = 9;
list.push(11);
list.push(13);
list[0]; //result:3
list[4]; //result:11
list.length; //result:6
list.pop(); //result:13;删除最后一个值,并返回该值
list.length; //result:5
list.concat(15); //添加到最后
var strList = list.join('-'); //result:"3-5-7-9-11"
list = strList.split('-'); //result:["3", "5", "7", "9", "11"]
list.slice(1,4); //result:["5", "7", "9"]
list.toString(); //result:"3,5,7,9,11"
构成栈
var list = new Array();
list.push('a'); //入栈
list.push('b'); //入栈
list.push('c'); //入栈
list.pop(); //result:'c';出栈
list //result:["a", "b"]
构成队列
var list = new Array();
list.push('a'); //入队列
list.push('b'); //入队列
list.push('c'); //入队列
list.shift(); //result:"a";删除第一个值,并返回该值;出队列
list.unshift();
list //result:["b", "c"]
list.unshift('d');
list;//result:["d", "b", "c"]
排序(升序)
var list = ['red','blue','green','black'];
list.sort();
list; //["black", "blue", "green", "red"]
排序(降序)
var list = ['red','blue','green','black'];
list.sort();
list.reverse();
list; //["red", "green", "blue", "black"]
反转
var list = ['red','blue','green','black'];
list.reverse();
list; //["black", "green", "blue", "red"]
//splice()方法
var list = ['red','blue','green','black'];
//删除
list.splice(0,2);
list; //["green", "black"]
//添加
list.splice(0,0,'yellow','purple');
list; //["yellow", "purple", "green", "black"]
//替换
list.splice(2,4,'white','red');
list; //["yellow", "purple", "white", "red"]
//多个替换掉一个
list.splice(2,1,'blue','black');
list; //["yellow", "purple", "blue", "black", "red"]
var list = new Array();
var list = [];
现在来看些基本功能
var list = new Array(3,5);
list[2] = 7;
list[3] = 9;
list.push(11);
list.push(13);
list[0]; //result:3
list[4]; //result:11
list.length; //result:6
list.pop(); //result:13;删除最后一个值,并返回该值
list.length; //result:5
list.concat(15); //添加到最后
var strList = list.join('-'); //result:"3-5-7-9-11"
list = strList.split('-'); //result:["3", "5", "7", "9", "11"]
list.slice(1,4); //result:["5", "7", "9"]
list.toString(); //result:"3,5,7,9,11"
构成栈
var list = new Array();
list.push('a'); //入栈
list.push('b'); //入栈
list.push('c'); //入栈
list.pop(); //result:'c';出栈
list //result:["a", "b"]
构成队列
var list = new Array();
list.push('a'); //入队列
list.push('b'); //入队列
list.push('c'); //入队列
list.shift(); //result:"a";删除第一个值,并返回该值;出队列
list.unshift();
list //result:["b", "c"]
list.unshift('d');
list;//result:["d", "b", "c"]
排序(升序)
var list = ['red','blue','green','black'];
list.sort();
list; //["black", "blue", "green", "red"]
排序(降序)
var list = ['red','blue','green','black'];
list.sort();
list.reverse();
list; //["red", "green", "blue", "black"]
反转
var list = ['red','blue','green','black'];
list.reverse();
list; //["black", "green", "blue", "red"]
//splice()方法
var list = ['red','blue','green','black'];
//删除
list.splice(0,2);
list; //["green", "black"]
//添加
list.splice(0,0,'yellow','purple');
list; //["yellow", "purple", "green", "black"]
//替换
list.splice(2,4,'white','red');
list; //["yellow", "purple", "white", "red"]
//多个替换掉一个
list.splice(2,1,'blue','black');
list; //["yellow", "purple", "blue", "black", "red"]