Angular - How to fetch value recursively?




Asked on March 27, 2019
Hello,
I have location tree view . I am storing the values in json format in database table.
I want to pass one location name and fetch all its children locations and if its child locations have children means it should fetch those location names also. How to achieve this using recursive method. 
My json data is as follows:

{  
   "text":"India",
   "value":1,
   "locationCode":"001",
   "children":[  
      {  
         "text":"Karnataka",
         "value":37,
         "locationCode":"EX01",
         "disabled":"false",
         "children":[  
            {  
               "text":"Hubli",
               "value":38,
               "disabled":"false",
               "locationCode":"UBL01",
               "children":[  
                  {  
                     "text":"Vidyanagar",
                     "value":43,
                     "disabled":"false",
                     "locationCode":"VID01",
                     "children":[  

                     ]
                  }
               ]
            },
            {  
               "text":"Bangalore",
               "value":40,
               "disabled":"false",
               "locationCode":"BANG01",
               "children":[  
                  {  
                     "text":"Jaynagar",
                     "value":48,
                     "disabled":"false",
                     "locationCode":"JAY019",
                     "children":[  
                        {  
                           "text":"vidyagiri",
                           "value":56,
                           "disabled":"false",
                           "locationCode":"vidyagiri",
                           "children":[  

                           ]
                        }
                     ]
                  }
               ]
            },
            {  
               "text":"Mysore",
               "value":57,
               "disabled":"false",
               "locationCode":"MYS01",
               "children":[  

               ]
            }
         ]
      }
   ]
}


For example: If I pass location name as Bangalore then it should fetch the children of Bangalore location which is jaynagar and also the children of jaynagarwhich is vidyagiri.  Like this it should work. How to achieve this using recursion?
Can any please provide solution for solving this issue?

Thanks & Regards
Shilpa Kulkarni




Replied on March 29, 2019
Try following code.

   ngOnInit() {
        this.findData();
   }
   getJsonData() {
    return '{"text":"India","value":1,"locationCode":"001","children":[{"text":"Karnataka","value":37,"locationCode":"EX01","disabled":"false","children":[{"text":"Hubli","value":38,"disabled":"false","locationCode":"UBL01","children":[{"text":"Vidyanagar","value":43,"disabled":"false","locationCode":"VID01","children":[]}]},{"text":"Bangalore","value":40,"disabled":"false","locationCode":"BANG01","children":[{"text":"Jaynagar","value":48,"disabled":"false","locationCode":"JAY019","children":[{"text":"vidyagiri","value":56,"disabled":"false","locationCode":"vidyagiri","children":[]}]}]},{"text":"Mysore","value":57,"disabled":"false","locationCode":"MYS01","children":[]}]}]}';
   }   
    
   searchNode(text, currentNode) {
    let result;

    if (text == currentNode.text) {
        return currentNode;
    } else {
        for (let i = 0; i < currentNode.children.length; i++) {
           let currentChild = currentNode.children[i];
            result = this.searchNode(text, currentChild);
            if (result !== false) {
                return result;
            }
        }
        return false;
    }
   }  

   findData() {
    let node = JSON.parse(this.getJsonData());
    console.log(this.searchNode("Bangalore", node).children);
   }


Output

[  
   {  
      "text":"Jaynagar",
      "value":48,
      "disabled":"false",
      "locationCode":"JAY019",
      "children":[  
         {  
            "text":"vidyagiri",
            "value":56,
            "disabled":"false",
            "locationCode":"vidyagiri",
            "children":[  

            ]
         }
      ]
   }
]


Write Answer











©2024 concretepage.com | Privacy Policy | Contact Us