Skip to content

Instantly share code, notes, and snippets.

@IvanAdmaers
Created February 17, 2022 10:24
Show Gist options
  • Save IvanAdmaers/3bbff5badf089e6ef90693b15144821f to your computer and use it in GitHub Desktop.
Save IvanAdmaers/3bbff5badf089e6ef90693b15144821f to your computer and use it in GitHub Desktop.
getObjectProperty JS
const getObjectProperty = (object = {}, property = '') => {
const properties = Array.isArray(property) ? property : property.split('.');
const propertyName = properties[0];
const value = object[propertyName];
if (properties.length <= 1) {
return value;
}
if (value === null || typeof value !== 'object') {
return undefined;
}
return getObjectProperty(value, properties.slice(1));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment