Created
March 23, 2019 13:39
-
-
Save karl-gustav/36454c0deb5fe9cc87207f16f5d50cc4 to your computer and use it in GitHub Desktop.
javascript safe 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, ...args) { | |
let walker = obj; | |
for (const arg of args) { | |
if (walker && walker[arg]) { | |
walker = walker[arg]; | |
} else { | |
return; | |
} | |
} | |
return walker; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment