我試圖通過一個對象來確保沒有任何屬性是未定義的。我發現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
}
});
您正在檢查它是否爲'undefined'作爲字符串。丟失引號:'undefined' – mcranston18 2015-03-30 21:35:17
我不認爲這是正確的...... – 2015-03-30 21:35:44
@ mcranston18'typeof'總是返回一個字符串。 – Barmar 2015-03-30 21:36:25