Created
February 13, 2020 01:34
-
-
Save danielcardeenas/cf0622abb3d668eea89c7e75c7e0b40f to your computer and use it in GitHub Desktop.
List of list combinations
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
// thanks @david | |
var arrays = [[1], [1], [1,2], [1]]; | |
function combos(list, currentIndex = 0, combinations = [], current = []) { | |
if (currentIndex === list.length) { | |
combinations.push(current); | |
} else { | |
for (var item of list[currentIndex]) { | |
combos(list, currentIndex + 1, combinations, [...current, item]); | |
} | |
} | |
return combinations; | |
} | |
console.log(combos(arrays)); | |
// 0: [1, 1, 1, 1] | |
// 1: [1, 1, 2, 1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment