title: How to create data for Flat Array for Angular Tree using Javascript/Typescript
body: <p>I have the following JSON data -&gt;</p>
  <pre><code>{
    &quot;data&quot;: [
      {
        &quot;documentId&quot;: &quot;new_148_45646&quot;,
        &quot;data&quot;: &quot;new_data6&quot;
      },
      {
        &quot;documentId&quot;: &quot;new_145_456&quot;,
        &quot;data&quot;: &quot;new_data1&quot;
      },
      {
        &quot;documentId&quot;: &quot;new_148_4546&quot;,
        &quot;data&quot;: &quot;new_data2&quot;
      },
      {
        &quot;documentId&quot;: &quot;new_145_456&quot;,
        &quot;data&quot;: &quot;new_data3&quot;
      },
      
      {
        &quot;documentId&quot;: &quot;new_148_4546&quot;,
        &quot;data&quot;: &quot;new_data6&quot;
      }      
    ]
  }
  </code></pre>
  <p>I want to convert this to following flat tree data</p>
  <pre><code>{
      &quot;treeData&quot;: [
          {
              &quot;documentId&quot;: &quot;new_148_45646&quot;,
              &quot;expandable&quot;: true,
              &quot;level&quot;: 0           
            }, {
              &quot;documentId&quot;: &quot;new_148_45646&quot;,
              &quot;expandable&quot;: false,
              &quot;level&quot;: 1,
              &quot;data&quot;: &quot;new_data6&quot;
            },
            {
              &quot;documentId&quot;: &quot;new_145_456&quot;,
              &quot;expandable&quot;: true,
              &quot;level&quot;: 0           
            }, {
              &quot;documentId&quot;: &quot;new_145_456&quot;,
              &quot;expandable&quot;: false,
              &quot;level&quot;: 1,
              &quot;data&quot;: &quot;new_data1&quot;
            },
            {
              &quot;documentId&quot;: &quot;new_145_456&quot;,
              &quot;expandable&quot;: false,
              &quot;level&quot;: 1,
              &quot;data&quot;: &quot;new_data3&quot;
            },
            {
              &quot;documentId&quot;: &quot;new_148_4546&quot;,
              &quot;expandable&quot;: true,
              &quot;level&quot;: 0           
            }, {
              &quot;documentId&quot;: &quot;new_148_4546&quot;,
              &quot;expandable&quot;: false,
              &quot;level&quot;: 1,
              &quot;data&quot;: &quot;new_data2&quot;
            },
            {
              &quot;documentId&quot;: &quot;new_148_4546&quot;,
              &quot;expandable&quot;: false,
              &quot;level&quot;: 1,
              &quot;data&quot;: &quot;new_data6&quot;
            }
      ]
  }
  </code></pre>
  <p>So if there is a single element with a unique documentId  in the original array then it will be converted
  to 2 nodes -&gt; One node with the documentid , level 0 ,expandable=true and another node node with same documentid, expandable=false, level 1, data . <br><br>
  If there are 2 elements with a same documentId in the original array then it will be converted
  to 3 nodes -&gt; One node with the documentid , level 0 ,expandable=true and 2 nodes with same documentid, expandable=false, level 1, data<br><br>
  If there are 3 elements with a same documentId in the original array then it will be converted
  to 4 nodes -&gt; One node with the documentid , level 0 ,expandable=true and 3 nodes with same documentid, expandable=false, level 1, data<br><br></p>
  <p>The data will always be till level 1 only. <br></p>
  <p>Could some one please help me with this. Thanks</p>