Skip to content

Instantly share code, notes, and snippets.

@oyvindkinsey
Created January 3, 2014 00:07
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 oyvindkinsey/8229857 to your computer and use it in GitHub Desktop.
Save oyvindkinsey/8229857 to your computer and use it in GitHub Desktop.
array permutations
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