[{name:'北京'}, {city:'西安'}],
[{name:'上海'},{city:'包头'}],
[{name:'广州'},{city:'福州'}],
[{name:'北京'},{city:'广州'}],
[{name:'广州'},{city:'太原'}],
[{name:'上海'},{city:'昆明'}]
]
//先用map结构记录
function arrayToMap(data) {
//非数组或数据长度为0不处理
if(!data instanceof Array || data.length == 0) {
return {}
}
var map = {}
for(var i=0i<data.lengthi++) {
if(!data[i] instanceof Array || data.length < 2) {
continue
}
var name = data[i][0].name
if(name != undefined) {
if(map[name] == undefined) {
map[name] = []
}
map[name].push(data[i])
}
}
return map
}
//把map转成数组
function mapToArray(data) {
if(data == undefined) {
return []
}
var array = []
for(p in data) {
array.push(data[p])
}
return array
}
//测试
var map = arrayToMap(college)
var array = mapToArray(map)
console.log(array)
var arr = []for (var i = 0 i < 3 i++) {
arr[i] = []
for (var j = 0 j < i+1 j++) {
arr[i][j] = []
for (var z = 0 z < 3 z++) {
var v = ''
if(z==0){
k = 'A'
v='abc'
}else if(z==1){
k = 'B'
v='2017'
}else if(z==2){
k = 'C'
v='aa'
}
arr[i][j][k] = v
}
}
}
console.log(arr) 望采纳,不懂请追问
三维数组其实想通了,不就是每个元素是二维数组的一维数组么……再深一点想,每个元素二维数组不又是每个元素是个一维数组的元素么……var arr1 = new Array()arr1[0] = "这是第一维数组第一个元素"arr1[1] = "这是第一维数组第二个元素"arr1[2] = "这是第一维数组第三个元素"var arr2 = new Array()arr2[0] = arr1var arr3 = new Array()arr3[0] = arr2alert(arr3[0][0][0]+"\n"+arr3[0][0][1]+"\n"+arr3[0][0][2])arr3[0][0]][1] = "改变后的第一维数组第二个元素"alert(arr3[0][0][0]+"\n"+arr3[0][0][1]+"\n"+arr3[0][0][2])自己执行一遍就明白了