Created
October 23, 2019 12:22
-
-
Save Dafrok/39cd684cb4b83a1918167180fbecbacf to your computer and use it in GitHub Desktop.
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
var reg1 = getRegExp('\[[\'\"]', 'g'); | |
var reg2 = getRegExp('[\'\"]\]', 'g'); | |
function deepGet(object, path, defaultValue) { | |
var pathAry = path.replace(reg1, '.').replace(reg2, '').split('.'); | |
if (pathAry[0] === '') { | |
pathAry.shift(); | |
} | |
return pathAry.reduce(function (o, k) { | |
return o && o[k]; | |
}, object) || defaultValue; | |
} | |
module.exports.deepGet = deepGet; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment