我正在尝试逐个检查对象的属性,以确保没有未定义的属性。我发现了这个问题和这个问题,并实现了以下代码,但它不起作用。
for (var property in p.properties) {
if (p.properties.hasOwnProperty(property)) {
if (typeof property == 'undefined') {
p.properties[property] = '';
//a breakpoint here will NOT be hit
}
}
}
然而,如果我明确检查其中一个我知道具有未定义值的元素,它就可以正常工作:
if(typeof p.properties.st == 'undefined') {
p.properties.st = '';
//a breakpoint here WILL be hit
}
以下是数据获取的方式:
$.getJSON("data/stuff.json", function (data) {
$.each(data.features, function (i, p) {
//checking for undefined properties here
}
});
'undefined'
。去掉引号:undefined
。 - mcranston18typeof
总是返回一个字符串。 - Barmar