0

This is the result I'm looking for:

Input : [ 
            [
                {a: [v]}, 
                {b: [v]}
            ],
            [
                {c: [v]},
                {d: [v]}
            ]
        ]

Output : [
            {
                a: [v],
                c: [v]
            },
            {
                a: [v],
                d: [v]
            },
            {
                b: [v],
                c: [v]
            },
            {
                b: [v],
                d: [v]
            }
         ]

First it is an array of arrays made with objects. Then it turns to an array of objects. Each combination can have one element of each original array only. My attempt is the following:

  arr.forEach((d, i) => {
    d.forEach((l, j) => {
      temp[j] ? temp[j].push(l) : temp[j] = [l];
    });
  });

But I haven't figured out yet how to do it.

j08691
  • 204,283
  • 31
  • 260
  • 272
forkfork
  • 415
  • 6
  • 22
  • Will every array entry object contain exactly two inner arrays, no more and no less? Or can there be an arbitrary number of inner arrays? – IceMetalPunk Sep 17 '19 at 18:32
  • There can be an arbitrary number of inner arrays @IceMetalPunk. – forkfork Sep 17 '19 at 18:33
  • 2
    Possible duplicate of [Cartesian product of multiple arrays in JavaScript](https://stackoverflow.com/questions/12303989/cartesian-product-of-multiple-arrays-in-javascript) – Eris Sep 17 '19 at 18:33

0 Answers0