I would like to get firstName
and lastName
properties from a whole user
object. I need to use a conditional statement too.
How to do something like that?
getUserById(id)
and getUserByAddress(id)
use the JavaScript find()
method that returns an element or undefined
.
let { firstName, lastName } = getUserById(id);
if ({ firstName, lastName } === undefined) {
{ firstName, lastName } = getUserByAddress(id);
}
return `${firstName} ${lastName}`;
const { firstName, lastName } = getUserById(id) || getUserByAddress(id) || {};
if (firstName && lastName) {
return `${firstName} ${lastName}`;
}
return "Unknown user";
If getUserById(id)
is falsy, getUserByAddress(id)
will be executed. If this is falsy, too, {}
will at least prevent throwing an error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With