Home > Mobile >  Build Tree Structure of the given flat array
Build Tree Structure of the given flat array

Time:09-13

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

Below is the given flat array

[{
  "L0": "India",
  "L0_ID": "IN",
  "L1": "Karnataka",
  "L1_ID": "KA",
  "L2": "BLR",
  "L3": "Mysore",
  "L4": "CHB"
},
{
  "L0": "India",
  "L0_ID": "IN",
  "L1": "Karnataka",
  "L1_ID": "KA",
  "L2": "BLR",
  "L3": "Hubli"
},
{
  "L0": "India",
  "L0_ID": "IN",
  "L1": "Karnataka",
  "L1_ID": "KA",
  "L2": "BLR",
  "L3": "Udupi"
},
{
  "L0": "India",
  "L0_ID": "IN",
  "L1": "Rajasthan",
  "L1_ID": "RJ",
  "L2": "Jaipur",
  "L3": "Shastri Nagar",
  "L4": "Lane One"
},
{
  "L0": "India",
  "L0_ID": "IN",
  "L1": "Rajasthan",
  "L1_ID": "RJ",
  "L2": "Jodhpur",
  "L3": "Shastri Nagar",
  "L4": "Lane One"
}] 

I want generate the desired tree array structure using javascript.

{
  "name": "India",
  "id": "IN",
  "child": [
    {
      "name": "Karnataka",
      "id": "KA",
      "child": [
        {
          "name": "BLR",
          "id": null,
          "child": [
            {
              "name": "Mysore",
              "id": null
            },
            {
              "name": "Hubli",
              "id": null
            },
            {
              "name": "Udupi",
              "id": null
            }
          ]
        }
      ]
    },
    {
      "name": "Rajasthan",
      "id": "RJ"
    }
  ]
}

This is what I have tried so far

 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("LEVEL ZERO => ", e.L0)
                                                return first.L0 === second.L0 
                                            }
                                            if (level == 1) {
                                                //console.log("LEVEL ONE => ", e.L0, e.L1)
                                                return first.L0 === second.L0 && first.L1 === second.L1
                                            }
                                            if (level == 2) {
                                                //console.log("LEVEL TWO => ", e.L0, e.L1, e.L2)
                                                return first.L0 === second.L0 && first.L1 === second.L1 && first.L2 === second.L2
                                            }
                                            if (level == 3) {
                                                //console.log("LEVEL THREE => ", e.L0, e.L1, e.L2, e.L3)
                                                return first.L0 === second.L0 && first.L1 === second.L1 && first.L2 === second.L2 && first.L3 === second.L3
                                                
                                            }
                                            if (level == 4) {
                                                //console.log("LEVEL FOUR => ", e.L0, e.L1, e.L2, e.L3, e.L4)
                                                return first.L0 === second.L0 && first.L1 === second.L1 && first.L2 === second.L2 && first.L3 === second.L3 && first.L4 === second.L4
                                                
                                            }
                                            if (level == 5) {
                                                //console.log("LEVEL FIVE => ", e.L0, e.L1, e.L2, e.L3, e.L4, e.L5)
                                                return first.L0 === second.L0 && first.L1 === second.L1 && first.L2 === second.L2 && first.L3 === second.L3 && first.L4 === second.L4 && first.L5 === second.L5
                                                
                                            }
                                            //return e[`L${level}`] ;
                                        })

                                        for (let index = 0; index < 6; index  ) {
                                            unique_records_l0.forEach(element => {
                                                    var obj = {}
                                                    obj.name = element[`L${level}`]
                                                    obj.id = element[`L${level}_mask`]
                                                    obj.children = []
                                                    if (level < 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}`] && e[`L${level 1}`] ;
                                        // })

                                        return unique_records_l0



                                    }

CodePudding user response:

You could take an iterative approach by finding the wanted name of a given level.

If not found take a new object and take this object for the next level.

const
    data = [{ L0: "India", L0_ID: "IN", L1: "Karnataka", L1_ID: "KA", L2: "BLR", L3: "Mysore", L4: "CHB" }, { L0: "India", L0_ID: "IN", L1: "Karnataka", L1_ID: "KA", L2: "BLR", L3: "Hubli" }, { L0: "India", L0_ID: "IN", L1: "Karnataka", L1_ID: "KA", L2: "BLR", L3: "Udupi" }, { L0: "India", L0_ID: "IN", L1: "Rajasthan", L1_ID: "RJ", L2: "Jaipur", L3: "Shastri Nagar", L4: "Lane One" }, { L0: "India", L0_ID: "IN", L1: "Rajasthan", L1_ID: "RJ", L2: "Jodhpur", L3: "Shastri Nagar", L4: "Lane One" }],
    tree = data.reduce((r, o) => {
        let i = 0,
            t = { children: r };
            
        while (`L${i}` in o) {
            const
                name = o[`L${i}`],
                id = o[`L${i}_ID`] || null;
            let item = (t.children ??= []).find(q => q.name === name);
            if (!item) t.children.push(item = { name, id });
            t = item;
            i  ;
        }
        return r;
    }, []);

console.log(tree);
.as-console-wrapper { max-height: 100% !important; top: 0; }

  • Related