楼上的答案确认是对的,再给你一个答案,我觉得我写的比楼上的啰嗦。
/*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 a=[1,2,3],b=[4,5,6],result//把a,b变成逗号分隔的字符串,然后按逗号拆分字符串为数组
result=(a.join(',')+','+b.join(',')).split(',')
//["1", "2", "3", "4", "5", "6"]
注:该方案仅对数字和小数有效,对象、字符串 不建议采用此方法,当然,强用此方法一定得不到想要的结果
如果是对象和字符串的话...
乖乖用循环吧
var a=[1,2,3],b=[4,5,6],i=-1,resultwhile(++i<b.length)a.push(b[i])