0

I am dealing with coordinates in a grid and I need to make a two dimensional array of all the possible combinations of two input arrays.

Say I have

Xcoords = [0, 10, 15, 20, 25, 30, etc];

and I have

Ycoords = [0, 10, 15, 20, 25, 30, etc];

I want to create a 2D array that contains two value array pairs of every combination in the two dimensional space (a square or rectangular canvas), like:

Coords = [[0, 5], [5, 5], [10, 5], etc, etc, etc]

The number of items in the 2D array would be [number of items in first array] * [number of items in the second array].

I'm sure I could probably do this with a lot of loops, but what's the most efficient way? I'd really rather not have a dependency like lodash.

Many thanks!

Eddie
  • 26,593
  • 6
  • 36
  • 58
heydon
  • 315
  • 3
  • 9

0 Answers0