Skip to content

Instantly share code, notes, and snippets.

@thecneu
Created April 26, 2014 00:09
Show Gist options
  • Save thecneu/11307628 to your computer and use it in GitHub Desktop.
Save thecneu/11307628 to your computer and use it in GitHub Desktop.
Find combinations of a list of arrays [ ['a], ['b'] ] = 'a' 'b' 'ab'
function getCombinations( list ) {
var set = [],
listSize = list.length,
combinationsCount = (1 << listSize),
combination;
for ( var i = 1; i < combinationsCount; i++ ) {
var combination = [];
for ( var j=0; j<listSize; j++ ) {
if ( (i & (1 << j)) ) {
combination.push( list[j] );
}
}
set.push(combination);
}
return set;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment