Created
October 11, 2021 18:52
-
-
Save chriskoelle/2eefd055ffbf4b50e9b2b600e1d1aeca to your computer and use it in GitHub Desktop.
Get a nested value from an 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
/** | |
* Get a nested object value by path. | |
* | |
* @example | |
* var obj = {foo: {bar: 'baz'}}; | |
* getNestedValue(obj, 'foo.bar'); // returns 'baz' | |
* | |
* @param {Object} obj The nested object. | |
* @param {String} path The object path. | |
* @return {any} The object value at the specified path. | |
*/ | |
export const getNestedValue = (obj, path) => | |
path.split('.').reduce((nested, key) => nested && nested[key], obj); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment