title: Build Tree Structure of the given flat array
body: <p>I want to convert the flat array structure to the parent child tree structure upto N levels. The keys L0, L1, L2, L3, L4, L5, L6 are denoted as the the levels and they can go upto max L6. I have some idea about the recursive function but not sure how this can be implemented</p>
  <p>Below is the given flat array</p>
  <pre><code>[{
    &quot;L0&quot;: &quot;India&quot;,
    &quot;L0_ID&quot;: &quot;IN&quot;,
    &quot;L1&quot;: &quot;Karnataka&quot;,
    &quot;L1_ID&quot;: &quot;KA&quot;,
    &quot;L2&quot;: &quot;BLR&quot;,
    &quot;L3&quot;: &quot;Mysore&quot;,
    &quot;L4&quot;: &quot;CHB&quot;
  },
  {
    &quot;L0&quot;: &quot;India&quot;,
    &quot;L0_ID&quot;: &quot;IN&quot;,
    &quot;L1&quot;: &quot;Karnataka&quot;,
    &quot;L1_ID&quot;: &quot;KA&quot;,
    &quot;L2&quot;: &quot;BLR&quot;,
    &quot;L3&quot;: &quot;Hubli&quot;
  },
  {
    &quot;L0&quot;: &quot;India&quot;,
    &quot;L0_ID&quot;: &quot;IN&quot;,
    &quot;L1&quot;: &quot;Karnataka&quot;,
    &quot;L1_ID&quot;: &quot;KA&quot;,
    &quot;L2&quot;: &quot;BLR&quot;,
    &quot;L3&quot;: &quot;Udupi&quot;
  },
  {
    &quot;L0&quot;: &quot;India&quot;,
    &quot;L0_ID&quot;: &quot;IN&quot;,
    &quot;L1&quot;: &quot;Rajasthan&quot;,
    &quot;L1_ID&quot;: &quot;RJ&quot;,
    &quot;L2&quot;: &quot;Jaipur&quot;,
    &quot;L3&quot;: &quot;Shastri Nagar&quot;,
    &quot;L4&quot;: &quot;Lane One&quot;
  },
  {
    &quot;L0&quot;: &quot;India&quot;,
    &quot;L0_ID&quot;: &quot;IN&quot;,
    &quot;L1&quot;: &quot;Rajasthan&quot;,
    &quot;L1_ID&quot;: &quot;RJ&quot;,
    &quot;L2&quot;: &quot;Jodhpur&quot;,
    &quot;L3&quot;: &quot;Shastri Nagar&quot;,
    &quot;L4&quot;: &quot;Lane One&quot;
  }] 
  </code></pre>
  <p>I want generate the desired tree array structure using javascript.</p>
  <pre><code>{
    &quot;name&quot;: &quot;India&quot;,
    &quot;id&quot;: &quot;IN&quot;,
    &quot;child&quot;: [
      {
        &quot;name&quot;: &quot;Karnataka&quot;,
        &quot;id&quot;: &quot;KA&quot;,
        &quot;child&quot;: [
          {
            &quot;name&quot;: &quot;BLR&quot;,
            &quot;id&quot;: null,
            &quot;child&quot;: [
              {
                &quot;name&quot;: &quot;Mysore&quot;,
                &quot;id&quot;: null
              },
              {
                &quot;name&quot;: &quot;Hubli&quot;,
                &quot;id&quot;: null
              },
              {
                &quot;name&quot;: &quot;Udupi&quot;,
                &quot;id&quot;: null
              }
            ]
          }
        ]
      },
      {
        &quot;name&quot;: &quot;Rajasthan&quot;,
        &quot;id&quot;: &quot;RJ&quot;
      }
    ]
  }
  </code></pre>
  <p>This is what I have tried so far</p>
  <pre><code> function generate_tree_map(level,data) {
                                       //Get ALL UNIQUE RECORDS AT GIVEN LEVEL
                                         // var unique_records_l0 = _.uniqBy(response, function (e) {
                                          var unique_records_l0 = _.uniqWith(data, function(first, second) {
                                              if (level == 0) {
                                                  //console.log(&quot;LEVEL ZERO =&gt; &quot;, e.L0)
                                                  return first.L0 === second.L0 
                                              }
                                              if (level == 1) {
                                                  //console.log(&quot;LEVEL ONE =&gt; &quot;, e.L0, e.L1)
                                                  return first.L0 === second.L0 &amp;&amp; first.L1 === second.L1
                                              }
                                              if (level == 2) {
                                                  //console.log(&quot;LEVEL TWO =&gt; &quot;, e.L0, e.L1, e.L2)
                                                  return first.L0 === second.L0 &amp;&amp; first.L1 === second.L1 &amp;&amp; first.L2 === second.L2
                                              }
                                              if (level == 3) {
                                                  //console.log(&quot;LEVEL THREE =&gt; &quot;, e.L0, e.L1, e.L2, e.L3)
                                                  return first.L0 === second.L0 &amp;&amp; first.L1 === second.L1 &amp;&amp; first.L2 === second.L2 &amp;&amp; first.L3 === second.L3
                                                  
                                              }
                                              if (level == 4) {
                                                  //console.log(&quot;LEVEL FOUR =&gt; &quot;, e.L0, e.L1, e.L2, e.L3, e.L4)
                                                  return first.L0 === second.L0 &amp;&amp; first.L1 === second.L1 &amp;&amp; first.L2 === second.L2 &amp;&amp; first.L3 === second.L3 &amp;&amp; first.L4 === second.L4
                                                  
                                              }
                                              if (level == 5) {
                                                  //console.log(&quot;LEVEL FIVE =&gt; &quot;, e.L0, e.L1, e.L2, e.L3, e.L4, e.L5)
                                                  return first.L0 === second.L0 &amp;&amp; first.L1 === second.L1 &amp;&amp; first.L2 === second.L2 &amp;&amp; first.L3 === second.L3 &amp;&amp; first.L4 === second.L4 &amp;&amp; first.L5 === second.L5
                                                  
                                              }
                                              //return e[`L${level}`] ;
                                          })
  
                                          for (let index = 0; index &lt; 6; index++) {
                                              unique_records_l0.forEach(element =&gt; {
                                                      var obj = {}
                                                      obj.name = element[`L${level}`]
                                                      obj.id = element[`L${level}_mask`]
                                                      obj.children = []
                                                      if (level &lt; 6) {
                                                          level = level + 1
                                                          obj.children = generate_tree_map(level, response)
                                                      }
                                                      tree.push(obj)
                                                  });
                                           }
  
                                          // var unique_records_l1 = _.uniqBy(data, function (e) {
                                          //     return e[`L${level}`] &amp;&amp; e[`L${level+1}`] ;
                                          // })
  
                                          return unique_records_l0
  
  
  
                                      }
  </code></pre>