Home » Javascript » Recursive function returns null

Recursive function returns null

Posted by: admin November 1, 2017 Leave a comment


I have a function that loops through each child of a tree and if the given id is matched with any node in tree it returns that node. I have made sure that the given id exist in a tree and the base case is reached and even if do console.log then it print the correct results but it returns only null.Any help will be highly appreciated.

loop.getNode = function (id, data) {
    var p = null;
    for (var i=0; i < data.length ; i++){
        if ( data[i]['event_class_id'] == id ){
            data[i]['is_removable'] = false;
            p = data[i];
        else {
            if ( data[i].children != undefined && data[i].children.length != 0) {
                if ( p != null){
                    loop.getNode(id, data[i].children);
    return p;

// calling it like var parent_node = loop.getNode(removables[index],data);