Created
June 5, 2018 08:38
-
-
Save cyrilf/0f71986a9d5c20ec2637022512d9c924 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
// Utility function to get access nested object properties securely | |
// | |
// Usage: | |
// const invoiceName = get(account, "user.invoiceAddress.name") | |
// | |
// Instead of: | |
// const invoiceName = (account && account.user && account.user.invoiceAddress && account.user.invoiceAddress.name) || null | |
// https://stackoverflow.com/a/23809123/1410020 | |
const get = (obj, key) => | |
key | |
.split(".") | |
.reduce((o, x) => (typeof o == "undefined" || o === null) ? o : o[x], obj) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment