作者:笨蚂蚁88 | 来源:互联网 | 2023-10-10 13:17
多个数组(数量不定)例如三个数组 {a,b} {1,2} {d}排列组合后为a,1,da,2,db,1,db,2,d是js的算法哦 var arr [["a","b"],["1","2"],
多个数组(数量不定)
例如三个数组 {a,b} {1,2} {d}
排列组合后为
a,1,d
a,2,d
b,1,d
b,2,d
是js的算法哦
var
arr = [[
"a"
,
"b"
],[
"1"
,
"2"
],[
"d"
]];
var
sarr = [[]];
for
(
var
i = 0; i < arr.length; i++) {
var
tarr = [];
for
(
var
j = 0; j < sarr.length; j++)
for
(
var
k = 0; k < arr[i].length; k++)
tarr.push(sarr[j].concat(arr[i][k]));
sarr = tarr;
}
alert(sarr.join(
"\n"
));