Created
December 18, 2012 22:41
-
-
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.
This file contains 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
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