Last active
September 11, 2018 09:32
-
-
Save joaocarmo/49e183a28f3d0c562f4057b3f1716cd5 to your computer and use it in GitHub Desktop.
Get nested value from an object
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
/* | |
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