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

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -