Skip to content

Instantly share code, notes, and snippets.

@davoclavo
Created December 18, 2012 22:41
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 davoclavo/4332763 to your computer and use it in GitHub Desktop.
Save davoclavo/4332763 to your computer and use it in GitHub Desktop.
Get a property from an object using jpath notation (I don't even know if that notation exists, but it is like 'object.property1.property2'). I use it to get properties of unknown objects that may or may not have a property, and they raise an exception.
getJpath = function(object, path){
var segments = path.split('.');
var first = segments.shift(); // and it removes it from the array
if( object[first] ) {
if( segments.length > 0 ) {
return getJpath(object[first], segments.join('.'));
} else {
return object[first];
}
} else {
return null;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment