Skip to content

Instantly share code, notes, and snippets.

@patrickkunka
Created September 8, 2016 20:39
Show Gist options
  • Save patrickkunka/2d3e43f08bf83ec0f7335b6a281a91fb to your computer and use it in GitHub Desktop.
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
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