Created
April 22, 2018 10:58
-
-
Save Archakov06/0e2e6575e93af601a107dcb9410dcb00 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
// Source: https://github.com/zewish/oget | |
'use strict'; | |
export default (obj, path, def) => { | |
let res = path | |
.replace(/\[/g, '.') | |
.replace(/\]/g, '') | |
.replace(/^\./, '') | |
.split('.') | |
.reduce((prev, curr) => { | |
return prev && prev[curr] | |
}, obj); | |
return (res === undefined) | |
? def | |
: res; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment