Created
September 4, 2018 19:05
-
-
Save ponich/0c8daecc0c930e5b65d9c5500af04364 to your computer and use it in GitHub Desktop.
Get value from object by key
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
/** | |
* Get value from object by key | |
* @param obj | |
* @param key | |
* @param defaultValue | |
* @returns {*} | |
*/ | |
function getObjectValue(obj, key, defaultValue) { | |
defaultValue = defaultValue || null; | |
if (typeof obj === 'undefined') { | |
return defaultValue; | |
} | |
var _index = key.indexOf('.'); | |
if (_index > -1) { | |
var resultValue = getObjectValue( | |
obj[key.substring(0, _index)], | |
key.substr(_index + 1), | |
defaultValue | |
); | |
return resultValue; | |
} | |
return obj[key] || defaultValue; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment