數組直接量
var arr;arr = ['AA', 'BB', 'CC'];alert(arr.toLocaleString()); //AA, BB, CCarr = [11, 22, 33];alert(arr.toLocaleString()); //11.00, 22.00, 33.00/* 數組元素可以是任意類型, 包括數組本身 */arr = ['AA', 'BB', 123];alert(arr.toLocaleString()); //AA, BB, 123.00alert(arr[0]); //AAalert(arr[1]); //BBalert(arr[2]); //123/* 可先站定維數, 以後賦值 */arr = [ , , ];arr[0] = 11;arr[1] = 22;arr[2] = 33;alert(arr.toLocaleString()); //11.00, 22.00, 33.00/* 先給個空數組, 再指定維數 */arr = [];alert(arr.length); //0arr.length = 3;arr[0] = 11;arr[1] = 22;arr[2] = 33;alert(arr.toLocaleString()); //11.00, 22.00, 33.00/* 可用 length 屬性改變數組的大小 */arr = [1,2,3,4,5,6];arr.length = 2;alert(arr.toLocaleString()); //1.00, 2.00
數組對象的三種建立方法
var arr;arr = new Array(11, '22', true);alert(arr.toLocaleString()); //11.00, 22, truearr = new Array(3);arr[0] = 11;arr[1] = '22';arr[2] = true;alert(arr.toLocaleString()); //11.00, 22, truearr = new Array();arr.length = 3;arr[0] = 11;arr[1] = '22';arr[2] = true;alert(arr.toLocaleString()); //11.00, 22, true
join : 數組到字串
var arr, str;arr = new Array('a', 'b', 1, 2, 3);str = arr.join();alert(str); //a,b,1,2,3/* 可指定分隔字元 */str = arr.join(' * ');alert(str); //a * b * 1 * 2 * 3
reverse、sort : 倒置與排序
var arr;arr = new Array('c', 'd', 'e', 'a', 'b');arr.reverse();alert(arr.toLocaleString()); //b, a, e, d, carr.sort();alert(arr.toLocaleString()); //a, b, c, d, e
concat : 添加返回新數組
var arr;arr = [1, 2, 3]arr = arr.concat(4, 5);alert(arr.join()); //1, 2, 3, 4, 5
push、pop : 從尾部添加與刪除; push 返回新長度, pop 返回被刪除的元素
var arr;arr = [1, 2, 3, 4, 5]arr.pop();alert(arr.join()); //1,2,3,4arr.push(7, 8, 9);alert(arr.join()); //1,2,3,4,7,8,9arr.push(0, ['A', 'B']);alert(arr.join()); //1,2,3,4,7,8,9,0,A,Barr.pop();alert(arr.join()); //1,2,3,4,7,8,9,0arr.pop();alert(arr.join()); //1,2,3,4,7,8,9
unshift、shift : 從開始插入與刪除; unshift 返回新長度, shift 返回被刪除的元素
var arr;arr = [1, 2, 3]arr.unshift(5, 6);alert(arr.join()); //5,6,1,2,3arr.shift();arr.shift();alert(arr.join()); //1,2,3arr.unshift([5,6,7,8]);alert(arr.join()); //5,6,7,8,1,2,3arr.shift();alert(arr.join()); //1,2,3
splice : 刪除、替換、插入; 如果有刪除則返回被刪除元素的數組
var arr;arr = [1, 2, 3, 4, 5, 6, 7, 8]arr.splice(4, 3); //從第 4 個(0起)開始刪除 3 個alert(arr.join()); //1, 2, 3, 4, 8arr.splice(3, 3, 'A', 'B', 'C'); //從第 3 個(0起)開始替換 3 個alert(arr.join()); //1, 2, 3, A, B, Carr.splice(3, 0, 7, 8, 9); //從第 3 個(0起)開始插入 3 個; 插入時第二個參數給 0alert(arr.join()); //1, 2, 3, 7, 8, 9, A, B, C
slice : 提取子數組; 返回新數組
var arr;arr = [1, 2, 3, 4, 5, 6, 7, 8]arr = arr.slice(2, 5);alert(arr.join()); //3,4,5/* 參數為負表示從後面數 */arr = [1, 2, 3, 4, 5, 6, 7, 8]arr = arr.slice(2, -2);alert(arr.join()); //3,4,5,6arr = [1, 2, 3, 4, 5, 6, 7, 8]arr = arr.slice(-3, -1);alert(arr.join()); //6,7
toString、valueOf
var arr;arr = [1, 2, 3, 'A', 'B', 'C']alert(arr); //1,2,3,A,B,Calert(arr.toString()); //1,2,3,A,B,Calert(arr.valueOf()); //1,2,3,A,B,Calert(arr.toLocaleString()); //1.00, 2.00, 3.00, A, B, C
遍曆與類比的多維陣列
var arr;arr = [[1,2,3], [4,5,6], [7,8,9]];alert(arr[1][1]); //5arr = new Array(new Array(1,2,3), new Array(4,5,6), new Array(7,8,9));alert(arr[1][1]); //5alert(arr) //1,2,3,4,5,6,7,8,9for (i=0; i<arr.length; i++) { document.write(arr[i] + '<b>'); //1,2,3 / 4,5,6 / 7,8,9}arr = [1,2,3,4,5,6,7,8,9];for (i in arr) { document.write(arr[i] + '<br>'); //1 / 2 / 3 / 4 / 5 / 6 / 7 / 8 / 9}arr = new Array([1,2,3], [4,5,6], [7,8,9]);for (i in arr) { document.write(arr[i] + '<br>'); //1,2,3 / 4,5,6 / 7,8,9}for (i in arr) for (j in arr[i]) document.write(arr[i][j] + '<br>'); //1 / 2 / 3 / 4 / 5 / 6 / 7 / 8 / 9