calendar
とtpoint
のキーに値が関連付けられているかどうかを確認しようとしています。キーに値があるかどうかを調べる
最初に、calendar
とtpoint
のキーが含まれているかどうかを確認しようとしましたが、そのキーがそのままキーであることに気付きました。その理由は、calendar
とtpoint
のキーに値があるかどうかを確認する必要がある場合があるためです。
私の試行は、以下のpackageContents
オブジェクトの下です。
キーに値があるかどうかをどのように確認できるか知っていますか?
var packageContents = {
'packages': [
{
'price': '32',
'name': 'Gold Bundle Package',
'calendar': {
'type': '2year',
'color': 'Brushed Nickel',
},
'tpoint': {
'type': 'Gold',
'touches': '21',
'years': '7',
}
},
{
'price': '23',
'name': 'Bronze Bundle Package',
'calendar': {
'type': '2year',
'color': 'Brushed Nickel',
},
'tpoint': {
'type': 'Bronze',
'touches': '9',
'years': '7',
}
}
]
};
packageContents['packages'].forEach(function (bun) {
if (bun['calendar'].length >= 1 && bun['tpoint'].length >= 1) {
var bundleSet;
console.log(bundleSet);
console.log('Working');
}
});
編集:
var packageContents = {
'packages': [
{
'price': '23',
'name': 'Bronze Bundle Package',
'calendar': {
'type': '2year',
'color': 'Brushed Nickel',
},
'tpoint': {
'type': '',
'touches': '',
'years': '',
}
}
]
};
var bundleSet = '';
packageContents['packages'].forEach(function (obj) {
if (typeof obj.calendar !== 'undefined' && typeof obj.tpoint !== 'undefined') {
bundleSet = "Bundle";
}
else {
bundleSet = "not bundle"
}
console.log(bundleSet);
});
ありがとう!したがって、typeofは定義されていないか、定義されて返されていますか – Paul
@Paul 'typeof'は、JavaScriptの型(' 'オブジェクト' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' ''値がないときは "undefined"となります。 –
それではなぜそれが未定義であるのでしょうか?すべてのキーに値がありますか?ちょうど混乱。 – Paul