1const permutations = arr => {
2 if (arr.length <= 2) return arr.length === 2 ? [arr, [arr[1], arr[0]]] : arr;
3 return arr.reduce(
4 (acc, item, i) =>
5 acc.concat(
6 permutations([...arr.slice(0, i), ...arr.slice(i + 1)]).map(val => [
7 item,
8 ...val,
9 ])
10 ),
11 []
12 );
13};