-1

I have variable that can be null, {}.

if (this.data) {
     this.postaddressForm.get('index_for').setValue(this.data.index_for);
     this.postaddressForm.get('dopaddr_for').setValue(this.data.dopaddr_for);
     this.postaddressForm.get('region_id').setValue(this.data.region_id);
     this.postaddressForm.get('house_for').setValue(this.data.house_for);
     this.postaddressForm.get('locality_for').setValue(this.data.locality_for);
     this.postaddressForm.get('street_for').setValue(this.data.street_for);
}

Problem is that sometimes there is properties in object data. How to check it?

So, if there is no street_for it fails.

isherwood
  • 58,414
  • 16
  • 114
  • 157

1 Answers1

0

You can use || to set a default parameter, and .? to handle data being undefined:

this.data?.street_for || ""
Luke Storry
  • 6,032
  • 1
  • 9
  • 22