Last active
June 12, 2023 09:48
-
-
Save jpedroribeiro/8950ed3f10e505cadb7f62467714ab98 to your computer and use it in GitHub Desktop.
How to get a nested property of an object with dot notation
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
/* | |
* 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
Usage example: