Created
August 22, 2017 11:47
-
-
Save akovalev/209d778ca14edd723ae2b47b42cdb865 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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