方法concat()将创建并返回一个新数组,这个数组是将所有参数都添加到array中生成的。它并不修改array。如果要进行concat()操作的参数是一个数组,那么添加的是数组中的元素,而不是数组。
示例
var
a
=
[1,2,3]
a.concat(4,
5)
//返回
[1,2,3,4,5]
a.concat([4,5])
//返回
[1,2,3,4,5]
a.concat([4,5],[6,7])
//返回
[1,2,3,4,5,6,7]
a.concat(4,
[5,[6,7]])
//返回
[1,2,3,4,5,[6,7]]
楼上的答案确认是对的,再给你一个答案,我觉得我写的比楼上的啰嗦。
/*var datas = [
['红色', '蓝色'],
['XL'],
['图片', '文字']
]
*/
/*
var datas = [
['红色', '蓝色'],
['XL'],
['图片', '文字'],
[1,2,3,4,5,6,7,8]
]
var datas = [
['红色', '蓝色'],
['XL','MS'],
['图片', '文字']
]
*/
var datas = [
['红色', '蓝色'],
['XL','MS'],
['图片', '文字'],
['WIN','LINUX','UNIX','OSX']
]
var merge1 = function(arr1,arr2) {
var result = []
for(var i=0i<arr1.lengthi++) {
for(j=0j<arr2.lengthj++) {
var tmp = []
tmp.push(arr1[i])
tmp.push(arr2[j])
result.push(tmp)
}
}
return result
}
var merge2 = function(arr1,arr2) {
var result = []
for(var i=0i<arr2.lengthi++) {
for(var j=0j<arr1.lengthj++) {
var tmp = copy(arr1[j])
tmp.push(arr2[i])
result.push(tmp)
}
}
return result
}
var copy = function(arr) {
var newArr = []
return newArr.concat(arr)
}
var iterator = function(data) {
return {
data:data,
len:data.length,
current:0,
hasNext:function(){
return this.current < this.len
},
next:function() {
if(!this.hasNext()) {
return null
}
var i = this.current
this.current++
return this.data[i]
}
}
}
var it = iterator(datas)
var a1=null,a2=null
while(it.hasNext()) {
if(a1 == null) {
a1 = it.next()
}
a2 = it.next()
if(a2 == null) {
break
}
if(a1[0] instanceof Array) {
a1=merge2(a1,a2)
}else{
a1 = merge1(a1,a2)
console.log(a1)
}
}
console.log(a1)
var oldarr=[
["1","2","3"],
["4","5","6"],
["7","8","9"]
]
var newarr=[]
let i=0
oldarr[0].forEach(e1=>{
oldarr[1].forEach(e2=>{
oldarr[2].forEach(e3=>{
newarr[i++]=e1+e2+e3
})
})
})
console.log(JSON.stringify(newarr))