如何删除数组中的空字段.. nodejs
我正在使用下面的代码…但如果状态是空的iss显示下面的结果在数据库中…我想删除如果状态为空..
1地址{},状态{}保存在数据库中。
function CheckStatus(oldJob, newJob) { var obj = {}; if(newJob && newJob.Status) { obj.Status= {}; if (oldJob.Status.total !== newJob.Status.total) { obj.Status.total = newJob.Status.total; } if (oldJob.Status.charge_description && oldJob.Status.charge_description !== newJob.Status.charge_description) { obj.Status.charge_description = newJob.Status.charge_description; } } }
MongoDB的
"Job" : { "_id" : ObjectId("5873b352e7621d08fccc5890"), "updated_by" : "", "details" : "{\"current_status\":\"Completed\",\"address\":{},\"Status\":{}}", "changetype" : "Update", "datetime" : ISODate("2017-01-09T15:59:14.202Z") },
请帮忙怎么进入If Condition(下面的代码不工作)
if(obj.address = '{}') { console.log('Empty'); }
将对象的值设置为undefined
实质上会删除它们。 你可以很容易地使用undefined
关键字:
newJob.address = undefined;
我将如何检查一个空的对象,并删除其内容:
if(Object.keys(newJob.address).length === 0 && obj.constructor === Object){ newJob.address = undefined; }
解答在这个问题的答案中更好地解释:
我如何testing一个空的JavaScript对象?