:
if (angular.isObject(place) && place.hasOwnProperty('opening_hours')) {
var openingHours = place.opening_hours;
if (angular.isObject(openingHours) && openingHours.hasOwnProperty('weekday_text')) {
$scope.input.opening_hours = openingHours.weekday_text;
}
else {
$scope.input.opening_hours = '';
}
}
else {
$scope.input.opening_hours = '';
}
純粋JSで:
// check if a object value
function isObject (_object) {
return Object.keys(_object).length >= 0 && _object.constructor === Object
}
if (isObject(place) && place.hasOwnProperty('opening_hours')) {
var openingHours = place.opening_hours;
if (isObject(openingHours) && openingHours.hasOwnProperty('weekday_text')) {
$scope.input.opening_hours = openingHours.weekday_text;
}
else {
$scope.input.opening_hours = '';
}
}
else {
$scope.input.opening_hours = '';
}
pathExists JS(基本)を使用して:三元
if (objectPathExists(place, 'opening_hours.weekday_text')) {
$scope.input.opening_hours = openingHours.weekday_text;
} else {
$scope.input.opening_hours = '';
}
:
$scope.input.opening_hours = objectPathExists(place, 'opening_hours.weekday_text') ? openingHours.weekday_text : ''
pathExists JS(試作品)の使用:
if (place.pathExists('opening_hours.weekday_text')) {
$scope.input.opening_hours = openingHours.weekday_text;
} else {
$scope.input.opening_hours = '';
}
は三元:
$scope.input.opening_hours = place.pathExists('opening_hours.weekday_text') ? openingHours.weekday_text : ''
opening_hours私は演算子の優先順位は、あなたが期待するものとは異なり、あなたが実際に実行していると思う未定義 – Mahi
です:? $ scope.input.opening_hours = place.opening_hours &&(place.opening_hours.weekday_text place.opening_hours.weekday_text : '';) –
カッコを使用します。しかし、なぜあなたはそのタスクを作る必要があるのか分かりませんが、元の 'if()'を読みやすくしています。 –