Skip to content

Instantly share code, notes, and snippets.

@mikemcbride
Created November 14, 2019 14:22
Show Gist options
  • Save mikemcbride/be033d27e4354f6f033959b5bc6c4e14 to your computer and use it in GitHub Desktop.
Save mikemcbride/be033d27e4354f6f033959b5bc6c4e14 to your computer and use it in GitHub Desktop.
Like _get from lodash but with zero dependencies
const _get = function(object, path, defaultVal) {
const _path = Array.isArray(path)
? path
: path.split('.').filter(i => i.length)
if (!_path.length || object === undefined) {
return object === undefined ? defaultVal : object
}
return _get(object[_path.shift()], _path, defaultVal)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment