How to rewrite this in one statement:
if (foo.bar !== undefined) {
const bee = foo.bar
}
I know there is the answer somewhere already but I can't find the right question in my mind.
How to rewrite this in one statement:
if (foo.bar !== undefined) {
const bee = foo.bar
}
I know there is the answer somewhere already but I can't find the right question in my mind.
You can try loadash npm package. But, under the hood, it does a similar check.
_.get(object, path, [defaultValue])
const bee = _.get('foo', 'bar', 'default');
const bee = _.get('foo', 'bar');
Full doc: https://lodash.com/docs/