title: Reorder a JS object in a child-parent structure
body: <p>I'm facing a brain-breaker (at leats for me) : 
  I get a json file with a one depth array of data objects like this:</p>
  
  <pre><code>[
  {"id":1, "name":"Sport", "parent_id":0, "children":[]},
  {"id":2, "name":"Tennis", "parent_id":4, "children":[]},
  {"id":3, "name":"Climbing", "parent_id":5, "children":[]},
  {"id":4, "name":"Indoor", "parent_id":1, "children":[]},
  {"id":5, "name":"Outdoor", "parent_id":1, "children":[]},
  {"id":6, "name":"Bowling", "parent_id":4, "children":[]}
  ]
  </code></pre>
  
  <p>how can I convert this in a tree-structure where children are put inside their parent's children array?  objects are not always in te right order, a child can come before its parent in the array. (like id 2 and 3 in my example)</p>
  
  <p>This is how I need it in the end:</p>
  
  <pre><code>[
  {"id":1, "name":"Sport", "parent_id":0, "children":
    [
      {"id":4, "name":"Indoor", "parent_id":1, "children":
      [
        {"id":2, "name":"Tennis", "parent_id":4, "children":[]},
        {"id":6, "name":"Bowling", "parent_id":4, "children":[]},
      ]},
      {"id":5, "name":"Outdoor", "parent_id":1, "children":
      [
        {"id":3, "name":"Climbing", "parent_id":5, "children":[]}
      ]},
    ]}
  ]
  </code></pre>
  
  <p>Any idea how to achieve this?</p>
  
  <p>I tried iterating over the elements and pushing the inside their parents children array, but when a parent has been moved, the next sibling can't find the parent anymore...</p>