Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 05-02-2013, 03:10 PM   #1
Registered: Dec 2010
Posts: 277

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.


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
Senior Member
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,293

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

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


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
Registered: Dec 2010
Posts: 277

Original Poster
Rep: Reputation: 24

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.


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

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...

function topdepth(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]'){
							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] ) {
		for ( var prop in obj){
			var value=obj[prop];
			if (value == findit){
				//if pat do another top depth
			if (typeof value == 'object'){
			} 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


javascript, json

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 > Forums > Non-*NIX Forums > Programming

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

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