理顺 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 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] + ''); //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] + ' '); //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] + ' '); //1,2,3 / 4,5,6 / 7,8,9 } for (i in arr) for (j in arr[i]) document.write(arr[i][j] + ' '); //1 / 2 / 3 / 4 / 5 / 6 / 7 / 8 / 9[object Object]