Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get the cartesian product of multiple sets
// Thanks to eddmann.com/posts/cartesian-product-in-javascript
const cartesianProduct = (...sets) =>
sets.reduce((sets, set) => sets.flatMap(x => set.map(y => [...x, y])), [[]]);
cartesianProduct(
['a', 'b', 'c'],
['x', 'y', 'z'],
);
// [
// ["a", "x"],
// ["a", "y"],
// ["a", "z"],
// ["b", "x"],
// ["b", "y"],
// ["b", "z"],
// ["c", "x"],
// ["c", "y"],
// ["c", "z"],
// ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.