Skip to content

Instantly share code, notes, and snippets.

@alextanhongpin
Created November 15, 2017 06:55
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 alextanhongpin/dacc5cf4143b34e6b42bd8a0dbf2f23d to your computer and use it in GitHub Desktop.
Save alextanhongpin/dacc5cf4143b34e6b42bd8a0dbf2f23d to your computer and use it in GitHub Desktop.
Create different combinations from array
function combination (x) {
return Array(Math.pow(2, x.length)).fill(0).map((_, i) => {
return Array(x.length).fill(0).map((_, j) => {
if ((i & Math.pow(2, j))) {
return x[j]
}
return null
}).filter((nonNull => nonNull !== null))
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment