-
-
Save aizigao/5dcbae08e653159846257c57b5d06cb4 to your computer and use it in GitHub Desktop.
Alternative to lodash get method _.get()
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
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