Menu

gitpiper

cartesianProduct javascript Code Snippet in 2023

matharraybeginner

Last Updated: 31 January 2023

Calculates the cartesian product of two arrays.

  • Use Array.prototype.reduce(), Array.prototype.map() and the spread operator (...) to generate all possible element pairs from the 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]]

javascript snippet similar to cartesianProduct For You in February 2023

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2023 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️