Skip to content

Instantly share code, notes, and snippets.

@whusterj
Created January 18, 2015 02:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whusterj/d7da3b9b04da9dd8fd01 to your computer and use it in GitHub Desktop.
Save whusterj/d7da3b9b04da9dd8fd01 to your computer and use it in GitHub Desktop.
Deep Get in JavaScript
function deepGet (obj, properties) {
// If we have reached an undefined/null property
// then stop executing and return undefined.
if (obj === undefined || obj === null) {
return;
}
// If the path array has no more elements, we've reached
// the intended property and return its value.
if (properties.length === 0) {
return obj;
}
// Prepare our found property and path array for recursion
var foundSoFar = obj[properties[0]];
var remainingProperties = properties.slice(1);
return deepGet(foundSoFar, remainingProperties);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment