Created
July 16, 2018 13:27
-
-
Save markmur/54982109f0f9c6e8ae740e58b1106567 to your computer and use it in GitHub Desktop.
Lookup a deeply nested object property by dot.notation
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
/** | |
* Lookup an object property by dot notation | |
* @param {Object} obj - object to perform lookup | |
* @param {String} key - property location | |
* @param {Any} fallback - fallback if not found | |
* @return {Any} returns value of lookup if found, otherwise undefined | |
*/ | |
const get = (obj, key, fallback) => | |
key | |
.split('.') | |
.reduce((state, x) => (state && state[x] ? state[x] : null), obj) || fallback |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment