JSFiddle - React, Tailwind, and code Playground
HTML
Cartesian product 2d array
JavaScript
var matrix = [[1,2,3], [4,5], [6,7,8],[9,10,11]];
console.log(cartesianProductOf([1,2,3], [4,5], [6,7,8],[9,10,11]));
//the following line does not produce the same output
console.log(cartesianProductOf(matrix));
function cartesianProductOf() {
return Array.prototype.reduce.call(arguments, function(a, b) {
var ret = [];
a.forEach(function(a) {
b.forEach(function(b) {
ret.push(a.concat([b]));
});
});
return ret;
}, [[]]);
}