Created
January 3, 2014 00:07
-
-
Save oyvindkinsey/8229857 to your computer and use it in GitHub Desktop.
array permutations
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
function array_permutate(set /*, additionalSets*/ ) { | |
var additionalSets = Array.prototype.slice.call(arguments, 1); | |
if (additionalSets.length === 0) { | |
return set.map(function (item) { return [item]; }); | |
} | |
var permutations = array_permutate.apply(null, additionalSets); | |
var result = []; | |
for (var i = 0; i < set.length; i++) { | |
for (var j = 0; j < permutations.length; j++) { | |
result.push([set[i]].concat(permutations[j])); | |
} | |
} | |
return result; | |
} | |
console.log(array_permutate( | |
[1, 2, 3], | |
['a', 'b', 'c'], | |
['A', 'B', 'C'] | |
)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment