title: Generate Tree from flat Array javascript
body: <p>I have an array as follow </p>
  
  <pre><code>arr = [
      {
          id: 1,
          name: 'Node1',
          level: 1
      },
      {
          id: 2,
          name: 'Node2',
          level: 2
      },
      {
          id: 3,
          name: 'Node3',
          level: 3
      },
      {
          id: 4,
          name: 'Node4',
          level: 4
      },
      {
          id: 5,
          name: 'Node5',
          level: 5
      },
  ];
  </code></pre>
  
  <p>I want to generate tree on basis of level.
  Output should be </p>
  
  <pre><code>tree = 
  {
      id: 1,
      name: 'Node1',
      level: 1,
      nodes: {
          id: 2,
          name: 'Node2',
          level: 2,
          nodes : {
              id: 3,
              name: 'Node3',
              level: 3,
              nodes: {
                  id: 4,
                  name: 'Node4',
                  level: 4,
                  nodes : {
                      id: 5,
                      name: 'Node5',
                      level: 5,
                      nodes: []
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>whenever nodes = [], that means it's the last nodes. the order of array can be anything but tree should be built on basis of level.</p>