Created
February 6, 2016 14:47
-
-
Save Fardinak/18249e8dd3c335ddd245 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
// Retrieve a deep item from obj, recursively | |
function getDeep (obj, addr) { | |
if(typeof obj !== 'object' && !Array.isArray(obj)) | |
throw new TypeError("The Object argument must be an Object or an Array"); | |
if(typeof addr !== 'string' && !Array.isArray(addr)) | |
throw new TypeError("The Address argument must be a string or and array"); | |
// Split the dotted address into an array | |
if(typeof addr === 'string') { | |
addr = addr.split('.'); | |
} | |
obj = obj[addr.splice(0, 1)[0]]; | |
// could also run obj() if it is a function | |
if(addr.length > 0 && obj != null) { | |
return getDeep(obj, addr); | |
} | |
return obj; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment