Created
August 29, 2019 07:21
-
-
Save srdjanRakic/a9547d40fa44e1e1a60d10e9916bef6d to your computer and use it in GitHub Desktop.
This feature enables readable and concise expression of property accesses with built-in nullish checking.
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
const object = { id: 123, names: { first: 'Srdjan', last: 'Rakic' }}; | |
// With lodash's `_.get`: | |
const firstName = _.get(object, 'names.first'); // -> 'Srdjan' | |
const middleName = _.get(object, 'names.middle' : '(no middle name)'); // -> '(no middle name)' | |
// with optional chaining and nullish coalescing: | |
const firstName = object?.names?.first ?? '(no first name)'; // -> 'Srdjan' | |
const middleName = object?.names?.middle ?? '(no middle name)'; // -> '(no middle name)' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment