Cross product of arrays

JavaScript, Math, Array · Oct 22, 2020

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

const xProd = (a, b) =>
  a.reduce((acc, x) => acc.concat(b.map(y => [x, y])), []);

xProd([1, 2], ['a', 'b']); // [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']]

More like this

  • JavaScript Arrays

    Master array manipulation in JavaScript with this ES6 snippet collection.

    Collection · 203 snippets

  • Product of numeric values

    Calculates the product of two or more numbers/arrays.

    JavaScript, Math · Oct 22, 2020

  • Cartesian product

    Calculates the cartesian product of two arrays.

    JavaScript, Math · Dec 29, 2020

  • Mapped array average

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

    JavaScript, Math · Oct 21, 2020