This function generates the cartesian product of two arrays.
JavaScript:
let cartesianProduct = (arr1, arr2) => arr1.flatMap(x => arr2.map(y => [x, y]));
// Example
console.log(cartesianProduct([1, 2], ['a', 'b'])); // [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']]
console.log(cartesianProduct(['x', 'y'], [10, 20])); // [['x', 10], ['x', 20], ['y', 10], ['y', 20]]
TypeScript:
let cartesianProduct = (arr1: any[], arr2: any[]): any[] => arr1.flatMap(x => arr2.map(y => [x, y]));
// Example
console.log(cartesianProduct([1, 2], ['a', 'b'])); // [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']]
console.log(cartesianProduct(['x', 'y'], [10, 20])); // [['x', 10], ['x', 20], ['y', 10], ['y', 20]]