Created
September 30, 2013 13:11
-
-
Save fudini/3baba5bd9229b545f650 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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