JavaScript ternary if x && y else -


$scope.input.opening_hours = place.opening_hours && place.opening_hours.weekday_text ? place.opening_hours.weekday_text : ''; 

uncaught typeerror: cannot read property 'weekday_text' of undefined(…)

if ( place.opening_hours && place.opening_hours.weekday_text ) {     $scope.input.opening_hours = place.opening_hours.weekday_text; } else {     $scope.input.opening_hours = ''; } 

i trying make ternary version of if statement error above. whats best way simplify simpler statement.

as tell in comments, need add parenthesis:

$scope.input.opening_hours = (place.opening_hours && place.opening_hours.weekday_text) ? place.opening_hours.weekday_text : ''; 

and can improve:

$scope.input.opening_hours = ((place.opening_hours && place.opening_hours.weekday_text) ? place.opening_hours.weekday_text : ''); 

the latter ensures inner parenthesis executed before all. it's maths, inner parenthesis needs executed before outer parenthesis.


Comments

Popular posts from this blog

Formatting string according to pattern without regex in php -

c - zlib and gdi32 with OpenSSL? -

java - inputmismatch exception -