Skip to content

Instantly share code, notes, and snippets.

@joaocarmo
Last active September 11, 2018 09:32
Show Gist options
  • Save joaocarmo/49e183a28f3d0c562f4057b3f1716cd5 to your computer and use it in GitHub Desktop.
Save joaocarmo/49e183a28f3d0c562f4057b3f1716cd5 to your computer and use it in GitHub Desktop.
Get nested value from an object
/*
Usage: getNestedValue(<object>, <array>)
const object = {
key1: {
key11: 'nested-value'
}
}
const nestedValue = getNestedValue(object, ['key1', 'key11'])
console.log(nestedValue)
> nested-value
*/
const getNestedValue = (nestedObj, pathArr) => pathArr.reduce((obj, key) => ((obj && obj[key] !== 'undefined') ? obj[key] : undefined), nestedObj)
export default getNestedValue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment