Skip to content

Instantly share code, notes, and snippets.

@Fardinak
Created February 6, 2016 14:47
Show Gist options
  • Save Fardinak/18249e8dd3c335ddd245 to your computer and use it in GitHub Desktop.
Save Fardinak/18249e8dd3c335ddd245 to your computer and use it in GitHub Desktop.
// 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