How to get a nested property of an object with dot notation
/* | |
* Returns the value of a property in a nested object. | |
* | |
* @param {Object} sourceObject | |
* @param {string} dotNotationPath | |
* @returns {*} | |
* | |
*/ | |
function getPropValue(sourceObject, dotNotationPath) { | |
let returnData = sourceObject; | |
dotNotationPath.split(".").forEach(subPath => { | |
returnData = returnData[subPath] || `Property ${subPath} not found`; | |
}); | |
return returnData; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
Usage example: