1

i am using below code...but if Status is Empty iss show below result in database...i want to remove if Status is Empty ..

1 Address {}, Status {} save in database.

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")
    },

please help how what enter in If Condition ( below code not working)

 if(obj.address = '{}')
  {
   console.log('Empty');

  }

1 Answer 1

1

Setting an object's value to undefined essentially deletes them. You may use the undefined keyword easily as such:
newJob.address = undefined;

How i would check for an empty object and delete its content:

if(Object.keys(newJob.address).length === 0 && obj.constructor === Object){
    newJob.address = undefined;
}

Solution is better explained in this question's answers:
How do I test for an empty JavaScript object?

Sign up to request clarification or add additional context in comments.

6 Comments

i was change my code.. if(obj.address = 'undefined') not working
Did you read the question i referenced? I only answered the part of "deleting" a variable. Also, you should check for undefined without quotation marks. And you never make comparisons with a single equals sign.
if(obj.address == undefined) not working when i enter if(obj.address = undefined) ..this working only empty ..
Thanks for answer ..its working :) Your Good Man :)
i need one more answer..please see my old post.. stackoverflow.com/questions/41533714/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.