我正在循環JSON文件的設置鍵。有時這些鍵的值將是空的(null?是空的適當的詞?)。因爲這會在以後的交互中產生錯誤。我想刪除這些空值以發送到val[]
。如何篩選出空的JSON鍵(空?)
Exampe JSON:
post_a: "THis is key 1"
post_b: "this is key 2"
post_c: "this is key 3"
post_d: ""
// I want to filiter out post_d because it is empty
循環:
keys = ["post_a", "post_b", "post_c", "post_d"];
val = [];
$.each(keys, function(i, key) {
val[i] = data[key];
return val[i];
});
目前,經過這個循環共完成:val.length = 4
如果這是工作打算:val.length = 3
_「empty(null?is null the proper word?)」__否,空字符串和null不是一回事。而你的例子「JSON」根本不是JSON。在函數中返回val [i];'也沒有意義。 – nnnnnn