JSFiddle - React, Tailwind, and code Playground
JavaScript
function cartesian_product(xs, ys) {
var result = [];
for(var i = 0; i < xs.length; i++) {
for (var j = 0; j < ys.length; j++) {
// transform [ [1, 2], 3 ] => [ 1, 2, 3 ] and append it to result []
result.push([].concat.apply([], [ xs[i], ys[j] ]));
}
}
return result;
}
function cartesian_power(xs, n) {
var result = xs;
for(var i = 1; i < n; i++) {
result = cartesian_product(result, xs)
}
return result;
}
// I need to have 30 numbers in the array and 7 as the second argument, and which is going to be a large number of output.
console.log(cartesian_power([1, 2, 3, 4], 2));