Created
May 9, 2017 05:26
-
-
Save bjdixon/ddaa821b189ce61631f258e82a7d0641 to your computer and use it in GitHub Desktop.
Use a path to get a nested value in an object. eg. obj.user.address.city == path(['user', 'address', 'city'], obj)
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
const isObject = item => item && typeof item === 'object' && !Array.isArray(item); | |
const path = ([...paths], obj) => { | |
const tmp = obj[paths.shift()]; | |
return !isObject(tmp) || !paths.length ? tmp : path(paths, tmp); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment