title: Convert array of objects with parent ids to a nested tree structure
body: <p>I have a mock JSON like below:</p>
  
  <pre><code>const apiData = [{
          "id": 1,
          "label": "List item 1",
          "parent_id": 0
      },
      {
          "id": 5,
          "label": "List item 1",
          "parent_id": 1
      },
      {
          "id": 6,
          "label": "List item 1",
          "parent_id": 1
      },
      {
          "id": 7,
          "label": "List item 1",
          "parent_id": 1
      },
      {
          "id": 8,
          "label": "List item 1",
          "parent_id": 1
      },
      {
          "id": 9,
          "label": "List item 1",
          "parent_id": 8
      },
      {
          "id": 10,
          "label": "List item 1",
          "parent_id": 8
      },
      {
          "id": 2,
          "label": "List item 1",
          "parent_id": 0
      }
  ]
  </code></pre>
  
  <p>and I need to convert it to below:</p>
  
  <pre><code>[{
          "id": 1,
          "label": "List item 1",
          "parent_id": 0,
          "children": [{
                  "id": 5,
                  "label": "List item 1",
                  "parent_id": 1
              },
              {
                  "id": 6,
                  "label": "List item 1",
                  "parent_id": 1
              },
              {
                  "id": 7,
                  "label": "List item 1",
                  "parent_id": 1
              },
              {
                  "id": 8,
                  "label": "List item 1",
                  "parent_id": 1,
                  "children": [{
                          "id": 9,
                          "label": "List item 1",
                          "parent_id": 8
                      },
                      {
                          "id": 10,
                          "label": "List item 1",
                          "parent_id": 8
                      }
                  ]
              }
  
          ]
      },
      {
          "id": 2,
          "label": "List item 1",
          "parent_id": 0
      }
  ]
  </code></pre>
  
  <p>The condition based on which we need to make changes is as follows:
  if corresponding to a particular id, if we have a <code>parent_id</code> then we need to add one property <code>children</code> in the individual object and put values as an array of matched <code>parent_id</code> object.</p>
  
  <p>I tried writing code for this and I came very close but I am unable to move forward.</p>
  
  <p>Kindly suggest.</p>
  
  <p>My code:</p>
  
  <pre><code>const apiData = [
  {'id': 1, 'label': 'List item 1', 'parent_id' : 0 },
  {'id': 5, 'label': 'List item 1', 'parent_id' : 1 },
  {'id': 6, 'label': 'List item 1', 'parent_id' : 1 },
  {'id': 7, 'label': 'List item 1', 'parent_id' : 1 },
  {'id': 8, 'label': 'List item 1', 'parent_id' : 1},
  {'id': 9, 'label': 'List item 1', 'parent_id' : 8 },
  {'id': 10, 'label': 'List item 1', 'parent_id' : 8 },
  {'id': 2, 'label': 'List item 1', 'parent_id' : 0 },
  ];
  
  function compare(a, b) {
    const idA = a.id;
    const idB = b.id;
    let comparison = 0;
    comparison = idA &gt; idB ? 1 : (idA &lt; idB ? -1 : 0);
    return comparison;
  }
  
  const sortedApiData = apiData.sort(compare);
  const newSortedApiData = [...sortedApiData];
  let a = []; 
  
  
  for(let i = 0; i &lt; sortedApiData.length ; i++){
    for(let j = 0 ; j &lt; sortedApiData.length ; j++){
       if(i === j){
         continue;
       }
       else{
         if(sortedApiData[i].id === sortedApiData[j].parent_id){
           a.push(sortedApiData[j]);
  
         }
       }
    }
  
  }
  
  console.log(a);
  
  </code></pre>