Cartesian product

JavaScript, Math, Array · Dec 29, 2020

Calculates the cartesian product of two arrays.

const cartesianProduct = (a, b) =>
  a.reduce((p, x) => [...p, => [x, y])], []);
cartesianProduct(['x', 'y'], [1, 2]);
// [['x', 1], ['x', 2], ['y', 1], ['y', 2]]

More like this

  • Calculates the product of two or more numbers/arrays.

  • Array sum

    JavaScript, Math

    Calculates the sum of two or more numbers/arrays.

  • Mapped array average

    JavaScript, Math

    Calculates the average of an array, after mapping each element to a value using the provided function.