Skip to content

Instantly share code, notes, and snippets.

@kpuputti
Created March 22, 2013 09:56
Show Gist options
  • Save kpuputti/5220197 to your computer and use it in GitHub Desktop.
Save kpuputti/5220197 to your computer and use it in GitHub Desktop.
function combinations(arr1, arr2) {
return arr1.reduce(function (prev, curr) {
return prev.concat(arr2.map(function (item) {
return [curr, item];
}));
}, []);
}
> combinations([1,2,3], ['a', 'b', 'c', 'd']);
[ [ 1, 'a' ],
[ 1, 'b' ],
[ 1, 'c' ],
[ 1, 'd' ],
[ 2, 'a' ],
[ 2, 'b' ],
[ 2, 'c' ],
[ 2, 'd' ],
[ 3, 'a' ],
[ 3, 'b' ],
[ 3, 'c' ],
[ 3, 'd' ] ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment