By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,575 Members | 1,413 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,575 IT Pros & Developers. It's quick & easy.

[Recursion] Always return null from recursion ?

P: 9
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:

Expand|Select|Wrap|Line Numbers
  1. function invokeRec () {
  2.  
  3.     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 
  4.    alert(v);  //
  5. }
  6.  
  7. function dataQuery(data,query) {
  8.  
  9.     if(typeof(data[query]) != "undefined") {
  10.          alert(data[query]) ;               // here is valid and then return ...
  11.                  return (data[query]);
  12.     }
  13.  
  14.     for(var something in data) {
  15.         if ( typeof(data[something]) == "object"
  16.             && typeof(data[something] != "undefined") 
  17.             && something != query) {
  18.             dataQuery(data[something],query);
  19.         }
  20.     }
  21. }
and here is the version2 :

Expand|Select|Wrap|Line Numbers
  1. function dataQuery(data,query) {
  2.     for(var something in data) {
  3.         if ( typeof(data[something]) == "object"
  4.             && typeof(data[something] != "undefined") 
  5.             && something != query) {
  6.             dataQuery(data[something],query);
  7.         } else if (something == query) {
  8.                       return (data[query]);
  9.                  }
  10.     }
  11. }
  12.  
what's the problem with these two snippets of code?
Thank you!
Nov 22 '07 #1
Share this Question
Share on Google+
2 Replies


gits
Expert Mod 5K+
P: 5,390
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 ...

Expand|Select|Wrap|Line Numbers
  1. var data  = { foo: 'bar', foobar: { foo: 'bar1' } };
  2. var query = 'foo';
  3. var res   = [];
  4.  
  5. function dataQuery(data, query) {
  6.     for(var key in data) {
  7.         if (typeof data[key] == "object" && key != query) {
  8.             dataQuery(data[key], query);
  9.         } else if (key == query) {
  10.             res.push(data[query]);
  11.         }
  12.     }
  13. }
  14.  
  15. dataQuery(data, query);
  16.  
  17. alert(res);
  18.  
so the entire thing only makes sense when you have multidimensional data-structures ... i hope this helps? otherwise please explain in more detail where you have particular problems?

kind regards
Nov 22 '07 #2

P: 9
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!!!
Nov 23 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.