const foo = [1, 2];
const bar = [10, 20];
const baz = [100, 200, 300];
const fooBarBaz = [foo, bar, baz];
const getAllCombinations = (arraysToCombine) => {
const divisors = [];
let combinationsCount = 1;
for (let i = arraysToCombine.length - 1; i >= 0; i--) {
divisors[i] = divisors[i + 1] ? divisors[i + 1] * arraysToCombine[i + 1].length : 1;
combinationsCount *= (arraysToCombine[i].length || 1);
}
const getCombination = (n, arrays, divisors) => arrays.reduce((acc, arr, i) => {
acc.push(arr[Math.floor(n / divisors[i]) % arr.length]);
return acc;
}, []);
const combinations = [];
for (let i = 0; i < combinationsCount; i++) {
combinations.push(getCombination(i, arraysToCombine, divisors));
}
return combinations;
};
const shortVersion = (...a) => a.reduce((a, b) => a.flatMap(d => b.map(e => [d, e].flat())));
function cartesianProduct (arr) {
return arr.reduce((a, b) => {
return a.map(x => {
return b.map(y => {
return x.concat(y)
})
}).reduce((c, d) => c.concat(d), [])
}, [[]])
}