Cartesian product of multiple arrays in JavaScript

viebel picture viebel · Sep 6, 2012 · Viewed 45.6k times · Source

How would you implement the Cartesian product of multiple arrays in JavaScript?

As an example,

cartesian([1, 2], [10, 20], [100, 200, 300]) 

should return

[
  [1, 10, 100],
  [1, 10, 200],
  [1, 10, 300],
  [2, 10, 100],
  [2, 10, 200]
  ...
]

Answer

rsp picture rsp · Mar 27, 2017

2020 Update: 1-line (!) answer with vanilla JS

Original 2017 Answer: 2-line answer with vanilla JS: (see updates below)

All of the answers here are overly complicated, most of them take 20 lines of code or even more.

This example uses just two lines of vanilla JavaScript, no lodash, underscore or other libraries:

let f = (a, b) => [].concat(...a.map(a => b.map(b => [].concat(a, b))));
let cartesian = (a, b, ...c) => b ? cartesian(f(a, b), ...c) : a;

Update:

This is the same as above but improved to strictly follow the Airbnb JavaScript Style Guide - validated using ESLint with eslint-config-airbnb-base:

const f = (a, b) => [].concat(...a.map(d => b.map(e => [].concat(d, e))));
const cartesian = (a, b, ...c) => (b ? cartesian(f(a, b), ...c) : a);

Special thanks to ZuBB for letting me know about linter problems with the original code.

Update 2020:

Since I wrote this answer we got even better builtins, that can finally let us reduce (no pun intended) the code to just 1 line!

const cartesian =
  (...a) => a.reduce((a, b) => a.flatMap(d => b.map(e => [d, e].flat())));

Special thanks to inker for suggesting the use of reduce.

Special thanks to Bergi for suggesting the use of the newly added flatMap.

Special thanks to ECMAScript 2019 for adding flat and flatMap to the language!

Example

This is the exact example from your question:

let output = cartesian([1,2],[10,20],[100,200,300]);

Output

This is the output of that command:

[ [ 1, 10, 100 ],
  [ 1, 10, 200 ],
  [ 1, 10, 300 ],
  [ 1, 20, 100 ],
  [ 1, 20, 200 ],
  [ 1, 20, 300 ],
  [ 2, 10, 100 ],
  [ 2, 10, 200 ],
  [ 2, 10, 300 ],
  [ 2, 20, 100 ],
  [ 2, 20, 200 ],
  [ 2, 20, 300 ] ]

Demo

See demos on:

Syntax

The syntax that I used here is nothing new. My example uses the spread operator and the rest parameters - features of JavaScript defined in the 6th edition of the ECMA-262 standard published on June 2015 and developed much earlier, better known as ES6 or ES2015. See:

The new methods from the Update 2020 example was added in ES2019:

It makes code like this so simple that it's a sin not to use it. For old platforms that don't support it natively you can always use Babel or other tools to transpile it to older syntax - and in fact my example transpiled by Babel is still shorter and simpler than most of the examples here, but it doesn't really matter because the output of transpilation is not something that you need to understand or maintain, it's just a fact that I found interesting.

Conclusion

There's no need to write hundred of lines of code that is hard to maintain and there is no need to use entire libraries for such a simple thing, when two lines of vanilla JavaScript can easily get the job done. As you can see it really pays off to use modern features of the language and in cases where you need to support archaic platforms with no native support of the modern features you can always use Babel, TypeScript or other tools to transpile the new syntax to the old one.

Don't code like it's 1995

JavaScript evolves and it does so for a reason. TC39 does an amazing job of the language design with adding new features and the browser vendors do an amazing job of implementing those features.

To see the current state of native support of any given feature in the browsers, see:

To see the support in Node versions, see:

To use modern syntax on platforms that don't support it natively, use Babel or TypeScript: