js配列の全配列アルゴリズム
521 ワード
/**
* @function
* [1,2,3] => 123 132 213 231 321 312
*/
function permutationCombination(arr) {
if (arr.length === 1) {
return [arr];
}
const res = [];
arr.forEach((val, index) => {
const childArr = [...arr];
childArr.splice(index, 1);
res.push(
...permutationCombination(childArr).map(item => [val, ...item])
);
});
return res;
}