Created
September 8, 2016 20:39
-
-
Save patrickkunka/2d3e43f08bf83ec0f7335b6a281a91fb to your computer and use it in GitHub Desktop.
A simple implementation of retrieving the value of an object's property via its stringkey
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
getProperty = function(obj, stringKey) { | |
var parts = stringKey.split('.'), | |
returnCurrent = null, | |
current = '', | |
i = 0; | |
if (!stringKey) { | |
return obj; | |
} | |
returnCurrent = function(obj) { | |
if (!obj) { | |
return null; | |
} else { | |
return obj[current]; | |
} | |
}; | |
while (i < parts.length) { | |
current = parts[i]; | |
obj = returnCurrent(obj); | |
i++; | |
} | |
if (typeof obj !== 'undefined') { | |
return obj; | |
} else { | |
return null; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment