I have the following object to which I wish to have a conditional property:
{
name: this.username,
DOB: new Date(this.inputDate)
}
Say, I wish to add a third property called gender if the user has specified their gender. What would the proper syntax for the following be:
{
name: this.username,
DOB: new Date(this.inputDate),
if(this.userGender) gender: this.userGender
}
P.S. I do not wish to have the gender property in my object if there is no value along with it. So how can I only create the property if the condition is satisfied?
let myObject = { /*object without gender */ }; if(this.userGender) myObject.gender = this.userGender;