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: - 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);
-
}
-
}
-
}
and here is the version2 : - 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]);
-
}
-
}
-
}
-
what's the problem with these two snippets of code?
Thank you!
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 ... -
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);
-
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
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!!!
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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;...
|
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;
|
by: Kay Schluehr |
last post by:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691
|
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...
|
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
|
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!
|
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);
}
|
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$) {
}
...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
| |