Created
March 23, 2021 20:29
-
-
Save dmwyatt/2f6a3ede2f093fd5c046bab59efb623b to your computer and use it in GitHub Desktop.
[nestedGet] Given list of strings, access nested properties in object.
This file contains hidden or 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
| /** | |
| * Given list of stings, access nested properties in object. | |
| * | |
| * @param {Object<string, *>} object | |
| * @param {Array<string>} accessors | |
| * @returns {*} | |
| */ | |
| function nestedGet(object, accessors) { | |
| return accessors.reduce((acc, prop) => { | |
| return acc?.[prop]; | |
| }, object); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment