function iteration(acc, head, rest) {
if (head) {
acc.add(head);
}
if (rest.length === 0) {
return;
}
const [next, ...nextRest] = rest;
for (const item of next) {
iteration(acc, `${head ? head + " " : ""}${item}`, nextRest);
}
}
function calculateAllCombinations(...arrays) {
console.log(arrays);
const acc = new Set();
iteration(acc, "", arrays);
return Array.from(acc);
}
let res = calculateAllCombinations(
["First", "Second"],
[1, 2, 3],
["X", "C", "Z"],
["m", "s"]
);
console.log(res);