Skip to content

Instantly share code, notes, and snippets.

@fudini
Created September 30, 2013 13:11
Show Gist options
  • Save fudini/3baba5bd9229b545f650 to your computer and use it in GitHub Desktop.
Save fudini/3baba5bd9229b545f650 to your computer and use it in GitHub Desktop.
/**
* Recursivelly find the first object with the given key
* @param {object} obj Object to search for key
* @param {string} key Key to find
* @return {object} Value if found, undefined if not found
*/
findValueForKey: function(obj, key) {
var value = undefined;
function find(obj, key) {
for(var k in obj) {
if(obj.hasOwnProperty(k)) {
if(k === key) {
value = obj[k];
return;
}
if(typeof obj[k] === 'object') {
arguments.callee(obj[k], key);
}
}
}
}
find(obj, key);
return value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment