After the clarification here is a recursive way to check that:
Here is a small implementation
function contains(obj,what){
if(obj === what){
return true;
}
if(typeof obj!== "object" || obj === null){
return false;
}
for(var i in obj){
if(contains(obj[i],what)){
return true;
}
}
return false;
}
This works in all browsers new and old and has no dependencies. Once again annotated:
function contains(obj,what){
if(obj === what){ // if the object is what we're looking for - return it
return true;
}
// if it's not what we're looking for and it's not an object return it
if(typeof obj!== "object" || obj === null){
return false;
}
for(var i in obj){ // check if it's any of its properties
if(contains(obj[i],what)){
return true; // found it in a child
}
}
return false; // did not find it anywhere.
}
Also, obligatory one liner:
function contains(obj,what){
return (obj === what)||((typeof obj === "object" && obj !== null) && Object.keys(obj).some(function(el){return contains(obj[el],what);}));
}