簡體   English   中英

檢查對象的所有屬性是否為 undefined

[英]Check all properties of object for undefined

我正在嘗試遍歷一個對象以確保沒有未定義的屬性。 我發現了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
        }
    }
}

但是,如果我明確檢查我知道具有未定義值的那個,它確實有效:

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')

您正在測試屬性名稱是否未定義,這是不可能的; 您想測試該是否未定義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM