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