JavaScript - 数组
1. 数组的声明
// 一维数组 var arr1 = ['1','2','3']; var arr2 = new Array('1','2','3'); var arr3 = new Array(); arr3[0] = '1'; arr3[1] = '2'; arr3[2] = '3'; // 字符串可以作为数组的索引, 遍历的时候需要用 for-each var arr4 = new Array(); arr3['一'] = '1'; arr3['二'] = '2'; arr3['三'] = '3'; // 二维数组 var arr5 = new Array(); arr5[0] = ['1','2','3']; arr5[1] = ['4','5','6'];
2. 遍历数组
var arr = ['1','2','3']; for (var i=0; i<arr.length; i++) { alert(arr[i]); } // for-each for (var i in arr) { alert(arr[i]); }