[英]Check all properties of object for undefined
我正在嘗試遍歷一個對象以確保沒有未定義的屬性。 我發現了this question和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')
您正在測試屬性名稱是否未定義,這是不可能的; 您想測試該值是否未定義。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.