Skip to content

Instantly share code, notes, and snippets.

@medfreeman
Forked from jeneg/lodashGetAlternative.js
Created May 24, 2017 14:24
Show Gist options
  • Save medfreeman/6512224bc27ad814a27b417742b7d4e3 to your computer and use it in GitHub Desktop.
Save medfreeman/6512224bc27ad814a27b417742b7d4e3 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