简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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