var a = [1,2,3]//may be a is [1,2,3,4,5,6...]
如何从变量a
显示二维数组?
[
[1,2,3],
[2,1,3],
[1,3,2],
[3,2,1]
]
这是我的代码:
var result = [];
for(i=0,i<Math.pow(a.length-1,2);i++){
var tmp = [];
tmp.push(...)
result.push(tmp)
};
console.log(result)
答案 0 :(得分:4)
您可以尝试以下方法,它将数组作为参数并返回包含array.length的per-d的二维数组
var permArr = [],
usedChars = [];
function permute(input) {
var i, ch;
for (i = 0; i < input.length; i++) {
ch = input.splice(i, 1)[0];
usedChars.push(ch);
if (input.length == 0) {
permArr.push(usedChars.slice());
}
permute(input);
input.splice(i, 0, ch);
usedChars.pop();
}
return permArr
};
console.log(permute([1, 2, 3]));