[Recursion] Always return null from recursion ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • michaelscript
    New Member
    • Nov 2007
    • 9

    [Recursion] Always return null from recursion ?

    hi ...
    here is a recursion , suppose it will return a value and
    but,,,when the function returned with a value ,and a var incept it,
    still it is null or undefined..
    the code here:

    [CODE=javascript]function invokeRec () {

    var v = dataQuery(data, query) ; //assume that data and query are available , i.e. data is an array with some elements and query is a valid term
    alert(v); //
    }

    function dataQuery(data, query) {

    if(typeof(data[query]) != "undefined" ) {
    alert(data[query]) ; // here is valid and then return ...
    return (data[query]);
    }

    for(var something in data) {
    if ( typeof(data[something]) == "object"
    && typeof(data[something] != "undefined" )
    && something != query) {
    dataQuery(data[something],query);
    }
    }
    }[/CODE]
    and here is the version2 :

    [CODE=javascript]function dataQuery(data, query) {
    for(var something in data) {
    if ( typeof(data[something]) == "object"
    && typeof(data[something] != "undefined" )
    && something != query) {
    dataQuery(data[something],query);
    } else if (something == query) {
    return (data[query]);
    }
    }
    }
    [/CODE]

    what's the problem with these two snippets of code?
    Thank you!
    Last edited by gits; Nov 22 '07, 08:08 AM. Reason: added code tags
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    hi ...

    have a look at the following example that uses your recursion, i made slight adaptions so that it makes sense to me. first data has to be an object not an array, except query would be a numerical index and not a string like in my example ... i use res to store a list of the found values for the query-string ...

    [CODE=javascript]
    var data = { foo: 'bar', foobar: { foo: 'bar1' } };
    var query = 'foo';
    var res = [];

    function dataQuery(data, query) {
    for(var key in data) {
    if (typeof data[key] == "object" && key != query) {
    dataQuery(data[key], query);
    } else if (key == query) {
    res.push(data[query]);
    }
    }
    }

    dataQuery(data, query);

    alert(res);
    [/CODE]
    so the entire thing only makes sense when you have multidimensiona l data-structures ... i hope this helps? otherwise please explain in more detail where you have particular problems?

    kind regards

    Comment

    • michaelscript
      New Member
      • Nov 2007
      • 9

      #3
      Hi,thanks for ur reply.
      Actually,i do iterate over json object as you did.
      And it's really a very complex data-structures,I supposed to create a query method that it could iterate over the data fields and find the suited value.The value returned may be an object or a string, and also could in deep-hiberarchy.So,I should recursively query each field that if it is a object type field,and this recursion nested in a loop. The issue is that if i added a 'return' keyword front of 'dataQuery(data[key], query);' , it will exit unexpectation. i.e. exiting form a recursion and break off the loop sentence outside ,still return an undefined value to me.But if remove the return sentence ,after finished loops,it still throw me an undefined value ,that is caused by recursion with no return word.To my eye,here you gave a approach to achieve my purpose,and it tells me that may be i should not count on returning value from a recursion such like this, may be there is A way.
      Thank you!!!

      Comment

      Working...