I\'m having trouble coming up with code to generate combinations from n number of arrays with m number of elements in them, in JavaScript. I\'ve seen similar questions about
Another implementation with ES6 recursive style
Array.prototype.cartesian = function(a,...as){ return a ? this.reduce((p,c) => (p.push(...a.cartesian(...as).map(e => as.length ? [c,...e] : [c,e])),p),[]) : this; }; console.log(JSON.stringify([0,1].cartesian([0,1,2,3], [[0],[1],[2]])));