我正在循环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