【发布时间】:2017-12-04 17:02:00
【问题描述】:
我正在尝试检查 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);
});
【问题讨论】:
标签: javascript jquery arrays object key