01.创建数组
var mArray = []; var mArray = new Array(); var mArray = new Array(size); var mArray = [element_0, element_1, ..., element_n]; var mArray = new Array(element_0, element_1, ..., element_n);
02.数组赋值
var mArray = new Array("AA"); mArray[0] = "BB"; //mArray:["BB"],mArray.length:1 mArray[1] = "CC"; //mArray:["BB", "CC"],mArray.length:2 mArray[3] = "DD"; //mArray:["BB", "CC", undefined, "DD"],mArray.length:4 var mArray = new Array("AA"); mArray.unshift("BB"); //mArray:["BB", "AA"],mArray.length:2 mArray.unshift("CC", "DD"); //mArray:["CC", "DD", "BB", "AA"],mArray.length:4 var mArray = new Array("AA"); mArray.push("BB"); //mArray:["AA", "BB"],mArray.length:2 mArray.push("CC", "DD"); //mArray:["AA", "BB", "CC", "DD"],mArray.length:4 var mArray = new Array("AA", "BB", "CC"); mArray.splice(1, 0, "DD", "EE"); //mArray:["AA", , "BB", "CC"] var mArray = new Array("AA"); mArray.length = 3; //mArray:["AA", undefined, undefined],mArray.length:3
03.删除数组元素
var mArray = new Array("AA", "BB"); var item = mArray.pop(); //mArray:["AA"],mArray.length:1,item:"BB" var item = mArray.pop(); //mArray:[],mArray.length:0,item:"AA" var item = mArray.pop(); //mArray:[],mArray.length:0,item:undefined var mArray = new Array("AA", "BB"); var item = mArray.shift(); //mArray:["BB"],mArray.length:1,item:"AA" var item = mArray.shift(); //mArray:[],mArray.length:0,item:"BB" var item = mArray.shift(); //mArray:[],mArray.length:0,item:undefined var mArray = new Array("AA", "BB", "CC", "DD", "EE"); var ret = mArray.splice(1, 2);//mArray:["AA", "DD", "EE"],ret:["BB", "CC"] var ret = mArray.splice(1); //mArray:["AA"],ret:["DD", "EE"] var mArray = new Array("AA", "BB", "CC", "DD", "EE"); mArray.length = 2; //mArray:["AA", "BB"],mArray.length:2
04.数组元素的遍历访问
var mArray = new Array("AA", "BB", "CC", "DD", "EE"); for (var i = 0; i < mArray.length; i++) { console.log(mArray[i]); } for (var i in mArray) { console.log("mArray[" + i.toString() + "] =", mArray[i]); }
05.数组操作
var mArray = new Array("AA", "BB", "CC", "DD", "EE"); var sArray = mArray.slice(1, 3); 此时mArray的值为:["AA", "BB", "CC", "DD", "EE"] 此时sArray的值为:["BB", "CC"] var mArray = new Array("AA", "BB"); var sArray = mArray.concat("CC", "DD", ["EE"], ["FF", "GG"], "HH"); 此时mArray的值为:["AA", "BB"] 此时sArray的值为:["AA", "BB", "CC", "DD", "EE", "FF", "GG", "HH"]
06.数组深拷贝
var mArray = new Array("AA", "BB", "CC", "DD", "EE"); var sArray = mArray.slice(0); mArray[0] = "FF"; sArray[0] = "GG"; 此时mArray的值为:["FF", "BB", "CC", "DD", "EE"] 此时sArray的值为:["GG", "BB", "CC", "DD", "EE"] var mArray = new Array("AA", "BB", "CC", "DD", "EE"); var sArray = mArray.concat(); mArray[0] = "HH"; sArray[0] = "II"; 此时mArray的值为:["HH", "BB", "CC", "DD", "EE"] 此时sArray的值为:["II", "BB", "CC", "DD", "EE"]
07.数组排序
var mArray = new Array("AA", "BB", "CC", "DD", "EE"); mArray.reverse(); //mArray:["EE", "DD", "CC", "BB", "AA"] var mArray = new Array("EE", "CC", "DD", "AA", "BB"); function sortAscending(a, b) { //该函数的作用使得数组中的元素按递增的顺序排序 return (a > b)?1:-1; } mArray.sort(sortAscending); //mArray:["AA", "BB", "CC", "DD", "EE"] var mArray = new Array("EE", "CC", "DD", "AA", "BB"); function sortDecreasing(a, b) { //该函数的作用使得数组中的元素按递减的顺序排序 return (a > b)?-1:1; } mArray.sort(sortDecreasing); //mArray:["EE", "DD", "CC", "BB", "AA"]
08.数组其他方法
简单类型示例: var mArray = new Array("AA", "BB", "CC", "DD", "EE"); var str = mArray.join("#"); //str:"AA#BB#CC#DD#EE" 复杂类型示例: function testItem(a, b) { this.a = a; this.b = b; } testItem.prototype.toString = function () { return "{" + this.a + "|" + this.b + "}"; } var aa = new testItem("AA", "BB"); var bb = new testItem("CC", "DD"); var mArray = new Array(aa, bb, "EE", "FF"); var str = mArray.join("#"); //str:"{AA|BB}#{CC|DD}#EE#FF"09.对使用数组的辅助说明
var mArray = new Array("AA", "BB"); function demoFunc(arr) { //arr作为传入的数组类型,函数内直接对原数组操作 arr.push("CC", "DD", "EE"); } demoFunc(mArray); //mArray:["AA", "BB", "CC", "DD", "EE"]
参考文档
1.
http://www.w3school.com.cn/js/js_obj_array.asp
2. http://www.w3school.com.cn/jsref/jsref_obj_array.asp