如何删除数组中的{}空字段.. nodejs
how to remove {} empty field in array .. nodejs
我正在使用下面的代码...但是如果状态为空则在数据库中显示以下结果...如果状态为空我想删除..
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 条件(下面的代码不起作用)
if(obj.address = '{}')
{
console.log('Empty');
}
将对象的值设置为 undefined
实际上会删除它们。您可以像这样轻松地使用 undefined
关键字:
newJob.address = undefined;
我将如何检查空对象并删除其内容:
if(Object.keys(newJob.address).length === 0 && obj.constructor === Object){
newJob.address = undefined;
}
解决方案在这个问题的答案中有更好的解释:
How do I test for an empty JavaScript object?
我正在使用下面的代码...但是如果状态为空则在数据库中显示以下结果...如果状态为空我想删除..
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 条件(下面的代码不起作用)
if(obj.address = '{}')
{
console.log('Empty');
}
将对象的值设置为 undefined
实际上会删除它们。您可以像这样轻松地使用 undefined
关键字:
newJob.address = undefined;
我将如何检查空对象并删除其内容:
if(Object.keys(newJob.address).length === 0 && obj.constructor === Object){
newJob.address = undefined;
}
解决方案在这个问题的答案中有更好的解释:
How do I test for an empty JavaScript object?