title: Convert an array of objects into a nested array of objects based on string property?
body: <p>I am stuck on a problem trying to convert a flat array of objects into a nested array of objects based the name property.</p>
  
  <p>What is the best way to convert the <code>input</code> array to resemble the structure of the <code>desiredOutput</code> array?</p>
  
  <pre><code>var input = [
      { 
          name: 'foo', 
          url: '/somewhere1',
          templateUrl: 'foo.tpl.html',
          title: 'title A', 
          subtitle: 'description A' 
      },
      { 
          name: 'foo.bar', 
          url: '/somewhere2', 
          templateUrl: 'anotherpage.tpl.html', 
          title: 'title B', 
          subtitle: 'description B' 
      },
      { 
          name: 'buzz.fizz',
          url: '/another/place',
          templateUrl: 'hello.tpl.html',  
          title: 'title C',  
          subtitle: 'description C' 
      },
      { 
          name: 'foo.hello.world', 
          url: '/',
          templateUrl: 'world.tpl.html',
          title: 'title D',   
          subtitle: 'description D' 
      }
  ]
  
  var desiredOutput = [
      {
          name: 'foo',
          url: '/somewhere1',
          templateUrl: 'foo.tpl.html',
          data: {
              title: 'title A',
              subtitle: 'description A'
          },
          children: [
              {
                  name: 'bar',
                  url: '/somewhere2', 
                  templateUrl: 'anotherpage.tpl.html',
                  data: {
                      title: 'title B', 
                      subtitle: 'description B'
                  }
              },
              {
                  name: 'hello',
                  data: {},
                  children: [
                      {
                          name: 'world',
                          url: '/',
                          templateUrl: 'world.tpl.html',
                          data: {
                              title: 'title D',   
                              subtitle: 'description D'
                          }
                      }
                  ]
              }
          ]
      },
      {
          name: 'buzz',
          data: {},
          children: [
              {
                  name: 'fizz',
                  url: '/',
                  templateUrl: 'world.tpl.html',
                  data: {
                      title: 'title C',   
                      subtitle: 'description C'
                  }
              }
          ]
      }
  ]
  </code></pre>
  
  <p>Note the order of the objects in the input array is not guaranteed.
  This code will be running in a Node.js environment and I am open to using libraries such as lodash to achieve the desired output.</p>
  
  <p>Any help is greatly appreciated.</p>