Skip to content

Instantly share code, notes, and snippets.

@chriskoelle
Created October 11, 2021 18:52
Show Gist options
  • Save chriskoelle/2eefd055ffbf4b50e9b2b600e1d1aeca to your computer and use it in GitHub Desktop.
Save chriskoelle/2eefd055ffbf4b50e9b2b600e1d1aeca to your computer and use it in GitHub Desktop.
Get a nested value from an object
/**
* 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