I am working with an array of arrays in TypeScript and I want to perform a specific action on every possible permutation of elements between the inner arrays.
For example:
const arrOfArrays = [
[1],
[2, 3, 4],
[5, 6],
[7, 8]
];
recursePermutations(arrOfArrays, permutation => {
console.log(permutation);
});
When this code is executed, the desired output should be as follows:
[ 1, 2, 5, 7 ]
[ 1, 2, 5, 8 ]
[ 1, 2, 6, 7 ]
[ 1, 2, 6, 8 ]
[ 1, 3, 5, 7 ]
[ 1, 3, 5, 8 ]
[ 1, 3, 6, 7 ]
[ 1, 3, 6, 8 ]
[ 1, 4, 5, 7 ]
[ 1, 4, 5, 8 ]
[ 1, 4, 6, 7 ]
[ 1, 4, 6, 8 ]
The callback for permutations should be called with the number of elements equal to the non-empty inner arrays present.
While there are questions on how to find permutations within a fixed number of arrays, my scenario involves an unknown number of inner arrays.