Created
April 26, 2014 00:09
-
-
Save thecneu/11307628 to your computer and use it in GitHub Desktop.
Find combinations of a list of arrays [ ['a], ['b'] ] = 'a' 'b' 'ab'
This file contains hidden or 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 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