Skip to content

Instantly share code, notes, and snippets.

@alinz
Forked from cybercase/product.js
Created February 22, 2019 18:40
Show Gist options
  • Save alinz/cad051bbbca828473a8b43ab2eb28fa5 to your computer and use it in GitHub Desktop.
Save alinz/cad051bbbca828473a8b43ab2eb28fa5 to your computer and use it in GitHub Desktop.
Python-like itertools.product function in javascript
function product() {
var args = Array.prototype.slice.call(arguments); // makes array from arguments
return args.reduce(function tl (accumulator, value) {
var tmp = [];
accumulator.forEach(function (a0) {
value.forEach(function (a1) {
tmp.push(a0.concat(a1));
});
});
return tmp;
}, [[]]);
}
console.log(product([1], [2, 3], ['a', 'b']));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment