Created
November 14, 2019 14:22
-
-
Save mikemcbride/be033d27e4354f6f033959b5bc6c4e14 to your computer and use it in GitHub Desktop.
Like _get from lodash but with zero dependencies
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 _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