Array.prototype.map()
to combine the element with each partial permutation, then Array.prototype.reduce()
to combine all permutations in one array.Array.prototype.length
equal to 2
or 1
.const permutations = arr => { if (arr.length <= 2) return arr.length === 2 ? [arr, [arr[1], arr[0]]] : arr; return arr.reduce( (acc, item, i) => acc.concat( permutations([...arr.slice(0, i), ...arr.slice(i + 1)]).map(val => [ item, ...val, ]) ), [] ); };
permutations([1, 33, 5]); // [ [1, 33, 5], [1, 5, 33], [33, 1, 5], [33, 5, 1], [5, 1, 33], [5, 33, 1] ]
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️