Skip to content

Instantly share code, notes, and snippets.

@derekedelaney
Forked from jeneg/lodashGetAlternative.js
Created November 27, 2017 16:46
Show Gist options
  • Save derekedelaney/08be1902cef5840d637a4f6493a7b746 to your computer and use it in GitHub Desktop.
Save derekedelaney/08be1902cef5840d637a4f6493a7b746 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