检查对象的所有属性是否未定义

Check all properties of object for undefined

我正在尝试遍历对象以确保 none 的属性未定义。我找到了 this question and this question 并实现了以下代码,但它不起作用。

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
    }
});

应该是:

if (typeof p.properties[property] == 'undefined')

您正在测试 属性 name 是否未定义,这是不可能的;你想测试 value 是否未定义。