在JSON.stringify()的输出中隐藏空值 [英] Hide null values in output from JSON.stringify()
本文介绍了在JSON.stringify()的输出中隐藏空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的代码,所有从一个Postgres表行的信息被当选择了特定ROWID字符串化.
In my code, all of the info from a Postgres table row are stringified when a specific rowID is selected.
var jsonRes = result.message.rows;
document.getElementById('panel').innerHTML = '<pre>' + JSON.stringify(jsonRes[0], null, "\t") + '</pre>'
结果看起来像这样:
{
"ogc_fid": 143667,
"relkey": 288007,
"acct": "000487000A0010000",
"recacs": "12.5495 AC",
"shape_star": 547131.567383,
"shape_stle": 3518.469618,
"objectid": 307755,
"zone_dist": "MU-3",
"pd_num": null,
"council_da": null,
"long_zone_": "MU-3",
"globalid": "{D5B006E8-716A-421F-A78A-2D71ED1DC118}",
"ord_num": null,
"notes": null,
"res_num": null,
"effectived": 1345766400000,
"shape.star": 629707.919922,
"shape.stle": 3917.657332,
"case_numbe": null,
"common_nam": null,
"districtus": null
}
我是JS的新手,想知道是否有一种简单的方法可以完全排除包含空值的列-一个大致如下所示的函数:
I am new to JS and would like to know if there might be a simple way to completely exclude the columns containing null values - a function that roughly looks like this:
function hide(jsonObject) {
if (property === null) {
hide property
} else {
return str
}
}
最后,面板中的对象看起来像这样:
So that in the end, the object in the panel looks like this:
{
"ogc_fid": 143667,
"relkey": 288007,
"acct": "000487000A0010000",
"recacs": "12.5495 AC",
"shape_star": 547131.567383,
"shape_stle": 3518.469618,
"objectid": 307755,
"zone_dist": "MU-3",
"long_zone_": "MU-3",
"globalid": "{D5B006E8-716A-421F-A78A-2D71ED1DC118}",
"effectived": 1345766400000,
"shape.star": 629707.919922,
"shape.stle": 3917.657332
}
推荐答案
感谢您的答复.我刚刚意识到JSON.stringify()具有一个REPLACER
参数(此处的信息)
Thanks for the replies. I just realized that JSON.stringify() has a REPLACER
parameter (info here)
所以我刚刚添加了:
function replacer(key, value) {
// Filtering out properties
if (value === null) {
return undefined;
}
return value;
}
document.getElementById('panel').innerHTML =
'<pre>' +
JSON.stringify(jsonRes[0], replacer, "\t") +
'</pre>'
;
这篇关于在JSON.stringify()的输出中隐藏空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文