Skip to content

Instantly share code, notes, and snippets.

@ponich
Created September 4, 2018 19:05
Show Gist options
  • Save ponich/0c8daecc0c930e5b65d9c5500af04364 to your computer and use it in GitHub Desktop.
Save ponich/0c8daecc0c930e5b65d9c5500af04364 to your computer and use it in GitHub Desktop.
Get value from object by key
/**
* 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