LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-02-2013, 03:10 PM   #1
cin_
Member
 
Registered: Dec 2010
Posts: 281

Rep: Reputation: 24
Javascript :: JSON :: Finding A Vaue


I am trying to write a javascript function that will take a value and search a JSON object to determine if the value exists anywhere in the JSON and then returns the highest element id that contains that value.

I will be unable to pass the id that contains the value to the function.

So...

JSONish ...

{
to_want : { present :{ firstperson: veux, secondperson : veux, thirdperson : veut, ...}
},
to_dance : {...},
...
}

the function should return, to_want when I pass it veux, as veux is an eventual child of the french verb to want.

Thanks for any help!

Last edited by cin_; 05-07-2013 at 11:12 AM. Reason: gramm´err
 
Old 05-03-2013, 07:37 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
I don't understand what it has to do with JSON. But the searching can be done like this:

Code:
var obj =  {
to_want : { present :{ firstperson: "veux", secondperson : "veux", thirdperson : "veut" } },
to_dance : { name : "dance" }
};

alert(searchObjectForValue(obj,"veut",null));

function searchObjectForValue(object,searchForValue,id)
{
	for (var name in object) {
		var value = object[name];
		if (typeof value == "object") {
			if (id == null) {
				id = name;
			}
			return searchObjectForValue(value,searchForValue,id);
		}
		if (typeof value == "string") {
			if (value == searchForValue) {
				return id;
			}
		}
	}
        return null;
}
 
Old 05-07-2013, 11:11 AM   #3
cin_
Member
 
Registered: Dec 2010
Posts: 281

Original Poster
Rep: Reputation: 24
Figured

Guttorm, thanks for the reply... the object that I am trying to search uses the javascript object notation, JSON?, so I thought to add the tag in case anyone knew of any built in functions that could do what I wanted.

And what I wanted was to find a value and return the highest level(terminology?) key associated with that value.

I ended up hacking together three functions. I am on a different computer right now, so I´ll put the code up later.

Essentially...

topdepth() :: the topdepth() function steps one by one through the highest level keys and sends each one individually to toarray()

toarray() :: toarray() does just that, it pushes all of the subordinate values to an array, if the value I want is found, it sends the first value, array[0], which was the key sent to

toarray(),
off to fulldepth()

fulldepth() :: fulldepth() takes the leveled object from toarray() prints it appropriately.

If you want to know any level above the value just iterate over topdepth() and toarray() until the desired depth is acquired.

It feels ugly, but any and all searching and documentation proved wanting in regard my needs...
Again, code to follow...

Code:
function topdepth(data,findit) {
		//alert(data+findit);
		if (data){
			$.each(data, function(key, val) {
				if ( val == '[object Object]') { finder(key,val,findit); }
			});
		}
} //topdepth() just iterate over highest, as sent, depth key
		
function fulldepth(data,findit,foundwhere) {
	if ( foundwhere) { hitems=hitems+"</br>================"+foundwhere+';</br>'; founder=0; }
	if (data){
		$.each(data, function(key, val) {
			if ( val == '[object Object]'){
					hitems=hitems+key+';</br>';
							fulldepth(val,findit); //leave out value so undefined during recursion
			} else { hitems=hitems+key+','+val+';</br>'; }
		});
	}
}//fulldepth() just iterate over highest,as sent, level key
		
function finder(key,obj,findit){
	var result=[];
	if ( !hitems.split(foundhere)[1] ) {
		
		result.push(key);
		for ( var prop in obj){
			var value=obj[prop];
			if (value == findit){
				onit=activity[result[0]];
				//if pat do another top depth
				fulldepth(onit,findit,result[0]);
				foundhere=result[0];
				break;
			} 
			if (typeof value == 'object'){
				result.push(finder(key,value,findit));
			} else { result.push(value); }
		}
	}
} //finder() place values in array, if verb found send first array value to fulldepth()
I guess I changed the function name of toarray() to finder()... hazzah
Also, it uses JQuery, less hazzah

Last edited by cin_; 05-09-2013 at 02:24 PM. Reason: gramm´err
 
  


Reply

Tags
javascript, json



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] JSON issues Tyler_H72 Programming 2 06-01-2012 06:49 AM
[SOLVED] echo javascript json array secretlydead Programming 2 02-11-2012 06:20 AM
What is Json and jQuery and how are they related to Javascript? Romanus81 Programming 1 08-13-2009 09:35 PM
json help sajith Programming 1 02-27-2008 06:38 AM
php-json fedora 5 hammer65 Fedora 0 07-15-2006 02:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration