数组的定义
var arr = [1, 2, 3, 4, "one", "two", "three", "four"]; //一维数组
var props = [["拳头", "刀", "枪"], ["boxing", "knife ", "gun"]]; //二维数组
调用
console.log(arr[0]);console.log(props[0][0]);
JS对数组的遍历
for (var i in arr) {//可能性不高 console.log(arr[i]);}for (var i = 0; i < arr.length; i++) { //性能差的 console.log(arr[i]);}for (var i = 0, max = arr.length; i < max; i++) {//性能好的 console.log(arr[i]);}
一维数组的合并为二维数组
var data = new Array();var datas = new Array();for( i=0; i<3; i++){data = [1,2,3]datas.push(data);} console.log(datas);
[[1, 2, 3], [1, 2, 3], [1, 2, 3]]