Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@danielcardeenas
Created February 13, 2020 01:34
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 danielcardeenas/cf0622abb3d668eea89c7e75c7e0b40f to your computer and use it in GitHub Desktop.
Save danielcardeenas/cf0622abb3d668eea89c7e75c7e0b40f to your computer and use it in GitHub Desktop.
List of list combinations
// 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