28

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?

2
  • 1
    Is there a reason you wouldn't just assign userGender conditionally? let myObject = { /*object without gender */ }; if(this.userGender) myObject.gender = this.userGender; Commented Jul 23, 2018 at 19:47
  • 1
    Its available in Dart - I wish it could be introduced into typescript Commented Oct 22, 2020 at 6:23

3 Answers 3

47

Ideally, you would just add the appropriate property as a second action after declaring your object. So something like:

const myObj = {
    name: this.username,
    DOB: new Date(this.inputDate),
}

if(this.userGender) myObj.gender = this.userGender;

However, sometimes it's nice to declare an "optional" property inline with the rest of them, in which case you can use object spread to get the effect you're looking for:

const myObj = {
    name: this.username,
    DOB: new Date(this.inputDate),

    ...this.userGender && { gender: this.userGender }
}
Sign up to request clarification or add additional context in comments.

4 Comments

I don't believe the first form works in TypeScript: Property 'gender' does not exist on type '{ name: string; DOB: Date; }'.
You can also use undefined instead of {} as { ...undefined } has the same effect as { ...{} } (empty object)
in which case, we can do ...this.userGender && { gender: this.userGender }
Adding properties to an already existing object at runtime is a bad idea because it slows down JavaScript. The reason is that in this case the JavaScript runtime environment will have build a history chain of the object. Why? Because you could access the object at different times resulting in different closures and versions of the object. Doing this a lot in your code is the biggest and most overlooked performance problem in applications.
25

it can be done like this too, more clean and readable.

const myObj = {
    name: this.username,
    DOB: new Date(this.inputDate),
    ...(this.userGender && { gender : this.userGender })
}

Comments

-1

Try this

let userObj = { name: this.username, DOB: new Date(this.inputDate) }
if(this.userGender) 
    userObj["gender"] = this.userGender;

3 Comments

What does :gender do on the last line? I can't see where that's described in any documentation.
This (the :gender part) doesn't appear to be valid TS.
Updated the answer, ignore : it was a normal string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.