Created
February 17, 2022 10:24
-
-
Save IvanAdmaers/3bbff5badf089e6ef90693b15144821f to your computer and use it in GitHub Desktop.
getObjectProperty JS
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
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