Javascript - Nested parent/child array recursive function that flattens parent only

Viewed 2588

I have a multidimensional nested parent/child array as follows:

{
  parent: {
     name: "foo"
  },
  children: [
    {
      parent: {
         name: "asdf",
      },
      children: []
    },
    {
     parent: {},
     children: []
    },
    {
     parent: {},
     children: []
    },
    ...
  ]
}

What I want to do is apply a recursive function on this array, to flatten only the parent property and keep children as-is, in order to get something like this:

 {
  name: "foo",
  children: [
    {
      name: "asdf",
      children: []
    },
    {
     children: []
    },
    {
     children: []
    },
    ...
  ]
}

I tried using underscore.js but I am unable to find a recursive function that could do the trick. Any thoughts?

2 Answers
Related