Skip to content

Instantly share code, notes, and snippets.

@markmur
Created July 16, 2018 13:27
Show Gist options
  • Save markmur/54982109f0f9c6e8ae740e58b1106567 to your computer and use it in GitHub Desktop.
Save markmur/54982109f0f9c6e8ae740e58b1106567 to your computer and use it in GitHub Desktop.
Lookup a deeply nested object property by dot.notation
/**
* 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