0

Please mark as duplicate if already solved.

The current structure : Array of Objects

const arrObj = [
  {
    head: 0,
    child_0: [
      { field: 0 },
      { field: 1 }
    ],
    child_1: [
      { field: 3 },
      { field: 4 },
      { field: 5 },
      { field: 6 }
    ],
    child_2: [
      { field: 7 },
      { field: 8 }
    ]
  }
]

Expected Output:

const newArrObj = [
  { head: 0, child_0: { field: 0 }, child_1: { field: 3 }, child_2: { field: 7 } },
  { head: 0, child_0: { field: 0 }, child_1: { field: 3 }, child_2: { field: 8 } },
  { head: 0, child_0: { field: 0 }, child_1: { field: 4 }, child_2: { field: 7 } },
  { head: 0, child_0: { field: 0 }, child_1: { field: 4 }, child_2: { field: 8 } },
  { head: 0, child_0: { field: 0 }, child_1: { field: 5 }, child_2: { field: 7 } },
  { head: 0, child_0: { field: 0 }, child_1: { field: 5 }, child_2: { field: 8 } },
  { head: 0, child_0: { field: 0 }, child_1: { field: 6 }, child_2: { field: 7 } },
  { head: 0, child_0: { field: 0 }, child_1: { field: 6 }, child_2: { field: 8 } },
  { head: 0, child_0: { field: 1 }, child_1: { field: 3 }, child_2: { field: 7 } },
  { head: 0, child_0: { field: 1 }, child_1: { field: 3 }, child_2: { field: 8 } },
  { head: 0, child_0: { field: 1 }, child_1: { field: 4 }, child_2: { field: 7 } },
  { head: 0, child_0: { field: 1 }, child_1: { field: 4 }, child_2: { field: 8 } },
  { head: 0, child_0: { field: 1 }, child_1: { field: 5 }, child_2: { field: 7 } },
  { head: 0, child_0: { field: 1 }, child_1: { field: 5 }, child_2: { field: 8 } },
  { head: 0, child_0: { field: 1 }, child_1: { field: 6 }, child_2: { field: 7 } },
  { head: 0, child_0: { field: 1 }, child_1: { field: 6 }, child_2: { field: 8 } },
]

Note that the array fields could be more than 2, as it is dynamic and have different lengths and property names. Been trying to figure this out for a day now.

Thanks

9
  • How many childs (child0 and child1) could be there? Commented Mar 13, 2018 at 6:34
  • @gurvinder372 it is dynamic so i can't tell, it could be many as possible. Commented Mar 13, 2018 at 6:37
  • In case of dynamic number of childs, how should the output looks like? Commented Mar 13, 2018 at 6:38
  • @gurvinder372 like the one on the output, as long as it sees an array field Commented Mar 13, 2018 at 6:46
  • ...so on - you've included sample input, please include exact required output ... and what you've tried Commented Mar 13, 2018 at 6:53

1 Answer 1

1

You can use reduce and a nested for loops.

I used spread here {...child_0[k1]} to clone the object and not just point by reference. If you just want to assign (point by reference), you can just use child_0[k1]

const arrObj = [{head: 0,child_0: [{ field: 0 },{ field: 1 }],child_1: [{ field: 3 },{ field: 4 },{ field: 5 }]}];
	
const newArrObj = arrObj.reduce((c,{ head, child_0, child_1 })=>{
   for ( var k1 in child_0 ) {
          for ( var k2 in child_1 ) {
               c.push({
                  head : head,
                  child_0 : {...child_0[k1]},
                  child_1 : {...child_1[k2]},
               });
          }
   }
   return c;
},[]);
  
console.log( newArrObj );


For Multiple child_*, I borrowed the permute() function on this post.

const arrObj = [{head: 0,child_0: [{ field: 0 },{ field: 1 }],child_1: [{ field: 3 },{ field: 4 },{ field: 5 }],child_2: [{ field: 55 },{ field: 66 }]}];
const newArrObj = arrObj.reduce((c, {head,...child}) => {
  var keys = Object.keys(child);

  function permute(input) {
    var out = [];

    (function permute_r(input, current) {
      if (input.length === 0) {
        out.push(current);
        return;
      }

      var next = input.slice(1);

      for (var i = 0, n = input[0].length; i != n; ++i) {
        permute_r(next, current.concat([input[0][i]]));
      }
    }(input, []));

    return out;
  }

  permute(Object.values(child)).forEach((v, i) => {
    let t = {head: head};
    v.forEach((x, k) => {t[keys[k]] = {...x};});
    c.push(t);
  });

  return c;
}, []);

console.log(newArrObj);

Sign up to request clarification or add additional context in comments.

1 Comment

And for child_2, child_3, …?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.