Cartesian product

JavaScript, Math · Dec 29, 2020

Calculates the cartesian product of two arrays.

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

More like this

  • Product of numeric values

    Calculates the product of two or more numbers/arrays.

    JavaScript, Math · Oct 22, 2020

  • Greatest common divisor

    Calculates the greatest common divisor between two or more numbers/arrays.

    JavaScript, Math · Dec 29, 2020

  • Cross product of arrays

    Creates a new array out of the two supplied by creating each possible pair from the arrays.

    JavaScript, Math · Oct 22, 2020