Skip to content

Instantly share code, notes, and snippets.

@aizigao
Forked from jeneg/lodashGetAlternative.js
Created June 11, 2018 01:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aizigao/5dcbae08e653159846257c57b5d06cb4 to your computer and use it in GitHub Desktop.
Save aizigao/5dcbae08e653159846257c57b5d06cb4 to your computer and use it in GitHub Desktop.
Alternative to lodash get method _.get()
function get(obj, path, def) {
var fullPath = path
.replace(/\[/g, '.')
.replace(/]/g, '')
.split('.')
.filter(Boolean);
return fullPath.every(everyFunc) ? obj : def;
function everyFunc(step) {
return !(step && (obj = obj[step]) === undefined);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment