Created
April 26, 2018 09:20
-
-
Save bbagdad/6764784602b8b84334bed9945434f41b to your computer and use it in GitHub Desktop.
Object.prototye.getValue
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
/* | |
* Gets the value of the object by using the keys passed as string. | |
* Returns the value of the specified key, null if undefined. | |
*/ | |
if (!Object.prototype.getValue) { | |
Object.prototype.getValue = function (key) { | |
var self = this; | |
if (!(self instanceof Object) || Object.keys(self).length == 0 || !key || typeof key !== 'string') { | |
return self; | |
} | |
if (key.indexOf('.') != -1) { | |
var subKey = key.substr(0, key.indexOf('.')); | |
if (self[subKey]) { | |
return self[subKey].getValue(key.substr(key.indexOf('.') + 1)) | |
} | |
} else { | |
return self[key]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage Example :