Considering this example:
if(this.plantService.plants[id])
{
if(this.plantService.plants[id].Name)
{
if(this.plantService.plants[id].Name[0])
return this.plantService.plants[id].Name[0].value;
else
return '';
}
else
return '';
}
return '';
I am wondering if it is possible to simplify what I am doing here.
My goal is to test the object-chain this.plantService.plants[id].Name[0] for validity.
However, if I just test if(this.plantService.plants[id].Name[0]) {...} exceptions are thrown.
Any proposals? :)
&&operator in your if like this:if(this.plantService.plants[id] && this.plantService.plants[id].Name && this.plantService.plants[id].Name[0]){return this.plantService.plants[id].Name[0].value} else {return ''}TypeError, as you'd be attempting to access a property onundefined.undefined