# 理顺 JavaScript (11) - 数组

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

/* 可先站定维数, 以后赋值 */
arr = [ , , ];
arr[0] = 11;
arr[1] = 22;
arr[2] = 33;
alert(arr.toLocaleString()); //11.00, 22.00, 33.00

/* 先给个空数组, 再指定维数 */
arr = [];
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;



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();

/* 可指定分隔符 */
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();

arr.push(7, 8, 9);

arr.push(0, ['A', 'B']);
arr.pop();
arr.pop();



unshift、shift : 从开始插入与删除; unshift 返回新长度, shift 返回被删除的元素
var arr;
arr = [1, 2, 3]
arr.unshift(5, 6);

arr.shift();
arr.shift();

arr.unshift([5,6,7,8]);
arr.shift();



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);

/* 参数为负表示从后面数 */
arr = [1, 2, 3, 4, 5, 6, 7, 8]
arr = arr.slice(2, -2);

arr = [1, 2, 3, 4, 5, 6, 7, 8]
arr = arr.slice(-3, -1);



toString、valueOf
var arr;
arr = [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]];

arr = new Array(new Array(1,2,3), new Array(4,5,6), new Array(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



posted on 2009-02-27 22:24  万一  阅读(...)  评论(...编辑  收藏