Files
30-seconds-of-code/snippets/xProd.md
Isabelle Viktoria Maciohsek 27c168ce55 Bake date into snippets
2021-06-13 13:55:00 +03:00

555 B

title, tags, firstSeen, lastUpdated
title tags firstSeen lastUpdated
xProd array,math,intermediate 2018-01-24T15:55:03+02:00 2020-10-22T20:24:44+03:00

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

  • Use Array.prototype.reduce(), Array.prototype.map() and Array.prototype.concat() to produce every possible pair from the elements of the two 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']]