javascript数组元素全排列
多个数组(数量不定)
例如三个数组 {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"
));