Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akovalev/209d778ca14edd723ae2b47b42cdb865 to your computer and use it in GitHub Desktop.
Save akovalev/209d778ca14edd723ae2b47b42cdb865 to your computer and use it in GitHub Desktop.
function generateAllCombinations(listsOfArgs) {
return listsOfArgs.reduce((acc, list) => mix(acc, list), []);
}
function mix(listA, listB) {
if (listA.length === 0) return listB;
if (listB.length === 0) return listA;
const result = [];
listA.forEach(itemA => {
listB.forEach(itemB => {
result.push(
(Array.isArray(itemA) ? itemA : [itemA]).concat(itemB)
);
})
});
return result;
}
console.log(
generateAllCombinations([
[ 'Robert', 'Ian', 'Nick' ], // firstName
[ 'Smith', 'Curtis', 'Johnson' ], // lastName
[ 23, 32, 45, 52 ], // age
[ 'France', 'Germany' ] // country
])
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment