在我的代码中,当选择特定的rowID时,Postgres表行中的所有信息都会被字符串化。
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的新手,想知道是否有一种简单的方法可以完全排除包含空值的列 - 这个函数大致如下所示:
function hide(jsonObject) {
if (property === null) {
hide property
} else {
return str
}
}
最后,面板中的对象如下所示:
{
"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
}
答案 0 :(得分:17)
您可以这样做:
let x = {
'x1':0,
'x2':null,
'x3':"xyz",
'x4': null
}
console.log(JSON.stringify(x, (key, value) => {
if (value !== null) return value
}))
&#13;
答案 1 :(得分:9)
感谢您的回复。我刚刚意识到JSON.stringify()有一个REPLACER
参数(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>'
;
答案 2 :(得分:0)
试试这个:
function getCleanObject(jsonObject) {
var clone = JSON.parse(JSON.stringify(jsonObject))
for(var prop in clone)
if(clone[prop] == null)
delete clone[prop];
return JSON.stringify(clone);
}
答案 3 :(得分:0)
如果你想保留你的初始对象,可以像这样创建一个新对象
var object = {
"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
};
var newObj = {};
Object.keys(object).forEach(function(key) {
if (object[key] !== null)
newObj[key] = object[key];
});
console.log(newObj);