473,387 Members | 1,573 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

[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:

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
2 1656
gits
5,390 Expert Mod 4TB
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
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

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

Similar topics

8
by: Jakle | last post by:
Hi all. Need alittle help here. This is an example from "How to Think Like a Computer Scientist: Learning with Python, Chapter 5". It's an open source ebook, so if you feel like it you can find it...
3
by: csx | last post by:
Hi all, Ive got a problem with recursion in Javascript. For this tree: http://www.pcm.uklinux.net/structure.jpg If you input node 3 (i.e. C) which is represented as 'values' in the array, it...
8
by: pembed2003 | last post by:
Hi all, As an exercise, I am trying to come up with a function to count the length of a char* string using recursion. I came up with this: int count_length(char* s){ if(s == 0) return 0;...
43
by: Lorenzo Villari | last post by:
I've tried to transform this into a not recursive version but without luck... #include <stdio.h> void countdown(int p) { int x;
19
by: Kay Schluehr | last post by:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691
18
by: MTD | last post by:
Hello all, I've been messing about for fun creating a trial division factorizing function and I'm naturally interested in optimising it as much as possible. I've been told that iteration in...
13
by: Mumia W. | last post by:
Hello all. I have a C++ program that can count the YOYOs that are in a grid of Y's and O's. For example, this Y O Y O O Y O Y O Y O O Y O Y Y O Y O Y O O Y O O Y Y O Y O
15
by: Gigs_ | last post by:
Can someone explain me this if l == : return else: return f(l) + l # <= cant figure this, how is all sum at the end? thanks!
17
by: AceKnocks | last post by:
I have been given this interface, interface BinarySearchTree { public void insert(Integer data); public int size(); public int height(); public boolean contains(Integer target); }
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.