Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to implement a recursive function in JQuery. Instead of returning a series of objects, the recursive function only returns one object(which makes me imply that it only do one recursive call).
JavaScript
//recursive function to find every Base Child 
function GetBaseChild(node)
{
    if(node.hasOwnProperty("children")){
        for(var i = 0; i < node.children.length; i++){
            return GetBaseChild(node.children[i]);
        }
    }else{
        return node;
    }
}


I am assuming that the return in my fall loop, stops my loop from going to the next index and do a recursion call hence the single call. Any idea how I can do it.

What I have tried:

Initially I solved the undefined error from this function. I tried adding an if statement inside the for loop to test if we are at the end of the loop and return the function but still it does not work like so.
//recursive function to find every Base Child 
function GetBaseChild(node)
{
    if(node.hasOwnProperty("children")){
        for(var i = 0; i < node.children.length; i++){
            if(i < node.children.length -1){
                  GetBaseChild(node.children[i]);
               }else{
                  return GetBaseChild(node.children[i]);
               }
        }
    }else{
        return node;
    }
}


This returns the last element of my expected result.
Posted
Updated 29-Jun-17 11:29am
Comments
Richard Deeming 29-Jun-17 16:46pm    
Whatever you put after the return keyword, that's what will be returned from your function.

Your function returns a single result. What were you expecting it to return?
Andrew Blessing Manyore 29-Jun-17 16:50pm    
I expect it to return about six objects from the data I have right now. Actually what I want it to do is to do a recursive call on each object inside the array.
Richard Deeming 29-Jun-17 17:08pm    
So do you want to return an array of all nodes?

Or to pass each node to a callback function?
Andrew Blessing Manyore 29-Jun-17 17:27pm    
Its not an array as such. I expect it to return values that I then add to an array, though I have used the array approach and it seems to be working.
Richard Deeming 29-Jun-17 17:32pm    
If you want to return multiple values, you don't really have a choice - you have to use an array.

The only other alternative would be to pass in a callback function, and have your GetBaseChild function call it for each node.
function GetBaseChild(node, callback) {
    callback(node);
    
    if(node.hasOwnProperty("children")) {
        for(var i = 0; i < node.children.length; i++) {
            GetBaseChild(node.children[i], callback);
        }
    }
}

1 solution

Solution

After browsing the Internet and reading some sources, I realized J Query has some limitations. My problem was being caused by the fact that when I use return then the control breaks out of the loop and the first/last result is returned depending on the logic I had used. And also considering the fact that J Query passes non-primitive types as references I decided to pass an array that my recursive function will manipulate and use it later in my code. Here is the source code.
//recursive function to find every Base Child
function GetBaseChild(array, node)
{
    if(node.hasOwnProperty("children")){
        for(var i = 0; i < node.children.length; i++){
            GetBaseChild(array, node.children[i]);
        }
    }else{
        array.push(node);
    }
}
 
Share this answer
 
Comments
PIEBALDconsult 29-Jun-17 20:08pm    
Please don't answer your own question. Use "Improve question" to add detail and context.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900