0

I have seen this somewhere but can't remember...

user.address= data.address? data.address : ""

instead of create an empty user.address field, I want to not create the field if the data.address is false

I'm using typescript.

Rafael Honda
  • 605
  • 2
  • 5
  • 13
  • Is there any reason not to just wrap it in a conditional? – Richard Jan 14 '20 at 18:59
  • not really, but how? like this..... `if (data.address) { user.address = data.address }` – Rafael Honda Jan 14 '20 at 19:22
  • Yes, that's what I'd recommend. – Richard Jan 15 '20 at 09:14
  • a conditional like that might work, but I know that there's another way to do it – Rafael Honda Jan 15 '20 at 12:26
  • 1
    You could assign the value to `undefined`, but then it'll still exist as a field in your object, which may not suit your needs. Alternatively you could make use of the spread operator as per https://stackoverflow.com/a/40560953/6262037 but it's not as idiomatic. – Richard Jan 15 '20 at 18:11

0 Answers0