理順 JavaScript (11) - 陣列

weixin_34262482發表於2009-02-27

陣列直接量
var arr;
arr = ['AA', 'BB', 'CC'];
alert(arr.toLocaleString()); //AA, BB, CC

arr = [11, 22, 33];
alert(arr.toLocaleString()); //11.00, 22.00, 33.00

/* 陣列元素可以是任意型別, 包括陣列本身 */
arr = ['AA', 'BB', 123];
alert(arr.toLocaleString()); //AA, BB, 123.00
alert(arr[0]);               //AA
alert(arr[1]);               //BB
alert(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);           //0
arr.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, true

arr = new Array(3);
arr[0] = 11;
arr[1] = '22';
arr[2] = true;
alert(arr.toLocaleString()); //11.00, 22, true

arr = 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, c

arr.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,4

arr.push(7, 8, 9);
alert(arr.join()); //1,2,3,4,7,8,9

arr.push(0, ['A', 'B']);
alert(arr.join()); //1,2,3,4,7,8,9,0,A,B
arr.pop();
alert(arr.join()); //1,2,3,4,7,8,9,0
arr.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,3

arr.shift();
arr.shift();
alert(arr.join()); //1,2,3

arr.unshift([5,6,7,8]);
alert(arr.join()); //5,6,7,8,1,2,3
arr.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, 8

arr.splice(3, 3, 'A', 'B', 'C'); //從第 3 個(0起)開始替換 3 個
alert(arr.join()); //1, 2, 3, A, B, C

arr.splice(3, 0, 7, 8, 9); //從第 3 個(0起)開始插入 3 個; 插入時第二個引數給 0
alert(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,6

arr = [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,C
alert(arr.toString());       //1,2,3,A,B,C
alert(arr.valueOf());        //1,2,3,A,B,C
alert(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]); //5

arr = new Array(new Array(1,2,3), new Array(4,5,6), new Array(7,8,9));
alert(arr[1][1]); //5

alert(arr) //1,2,3,4,5,6,7,8,9
for (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

相關文章