0

I have one 3D JavaScript array:

var arrayIn = [[[1, 2, 3], [4, 5, 6]],
             [[10, 11, 12],[1, 2]],
             [[21, 22], [1]]]

I want the output to be:

var arrayOut = [[1, 2, 3, 10, 11, 12, 21, 22],
                [1, 2, 3, 10, 11, 12, 1],
                [1, 2, 3, 1, 2, 21, 22],
                [1, 2, 3, 1, 2, 1],
                [4, 5, 6, 10, 11, 12, 21, 22],
                [4, 5, 6, 10, 11, 12, 1],
                [4, 5, 6, 1, 2, 21, 22],
                [4, 5, 6, 1, 2, 1]]

arrayIn can have more arrays and 2D arrays can be different sizes (does not have to be contain 2 array in it). How do I merge arrays like in the arrayOut?

Another example:

var arrayIn = [[[10, 11, 101]],
               [[1, 2, 3], [2, 3, 4]]]

Expected output:

var arrayOut = [[10, 11, 101, 1, 2, 3],
                [10, 11, 101, 2, 3, 4]]
Jantoma21
  • 395
  • 4
  • 10
  • I don't use javascript, but the former should be possible with a few for loops (or a data collection method) and the latter with some variation of an append or extend method. – Ariel A Dec 20 '20 at 14:51
  • you could take a standard approach for getting a cartesian product and if the code take `concat`, you get a flat arrays, like this [answer](https://stackoverflow.com/a/50631472/1447675). – Nina Scholz Dec 20 '20 at 18:24

0 Answers0