Something simple like this should work:
var testObj = {
test: 'testValue',
test1: 'testValue1',
test2: {
test2a: 'testValue',
test2b: 'testValue1'
}
}
function searchObj (obj, query) {
for (var key in obj) {
var value = obj[key];
if (typeof value === 'object') {
searchObj(value, query);
}
if (value === query) {
console.log('property=' + key + ' value=" + value);
}
}
}
If you execute searchObj(testObj, "testValue');
it will log the following to the console:
property=test value=testValue
property=test2a value=testValue
Obviously, you can replace the console.log
with whatever you want, or add a callback parameter to the searchObj
function to make it more reusable.
EDIT: Added the query
parameter which allows you to specify the value you want to search for when you call the function.