简体   繁体   English

检查对象的所有属性是否为 undefined

[英]Check all properties of object for undefined

I'm trying to step through an object to make sure none of the properties are undefined.我正在尝试遍历一个对象以确保没有未定义的属性。 I found this question and this question and implemented the following code, but it does not work.我发现了this questionthis 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
        }
    }
}

However, if I explicitly check the one that I know has undefined values, it does work:但是,如果我明确检查我知道具有未定义值的那个,它确实有效:

if(typeof p.properties.st == 'undefined') {
    p.properties.st = '';
    //a breakpoint here WILL be hit
}

Here is how the data is obtained:以下是获取数据的方式:

$.getJSON("data/stuff.json", function (data) {
    $.each(data.features, function (i, p) {
       //checking for undefined properties here
    }
});

It should be:它应该是:

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

You're testing whether the property name is undefined, which isn't possible;您正在测试属性名称是否未定义,这是不可能的; you want to test whether the value is undefined.您想测试该是否未定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM