title: How to create a tree structure from JSON data
body: <p>I have a list in JSON like this one:</p>
  
  <pre><code>var arr=[
  {
       "text":"text1",
       "id"  :"1",
       //no parent id
   },
  {
       "text":"text2",
       "id"  :"2",
       "idParent":"1"
   },
  {
       "text":"text3",
       "id"  :"3",
       "idParent":"2"
   },
  {
       "text":"text4",
       "id"  :"4",
       "idParent":"1"
   },
  {
       "text":"text5",
       "id"  :"5",
        //no parent id
   },
  ];
  </code></pre>
  
  <p>And I need to convert that list in a hierarchy tree like the following:</p>
  
  <pre><code>var arr = [
    {
      "text": "Parent 1",
      "id"  : "1",
      "nodes": [
        {
          "text": "Child 1",
          "id"  : "2",
          "parentid"  : "1",
          "nodes": [
            {
              "text": "Grandchild 1",
              "id"  : "4",
              "parentid"  : "2",
            },
            {
              "text": "Grandchild 2",
               "id"  : "8",
              "parentid"  : "2",
            }
          ]
        },
        {
          "text": "Child 2",
          "id"  : "10",
          "parentid"  : "1",
        }
      ]
    },
    {
      "text": "Parent 2",
      "id"  : "19",
      //no parent id
    }
  ];
  </code></pre>
  
  <p>Only the root parents have no "parentid" element. </p>
  
  <p>¿How can I do this in Javascript?</p>
  
  <p>Thanks in advance for your help.</p>