How to empty an JS Object?

14,134

Solution 1

I want to empty person object...I don't want to assign new empty object to person

I'm not aware of any kind of built-in object .deleteAllProperties() method, so that leaves looping through the properties and calling delete on each individually. Following is a reasonably tidy way to do that:

Object.keys(person).forEach(k => delete person[k])

Or the slightly longer non-ES6 arrow function version for support back as far as IE9:

Object.keys(person).forEach(function(k) { delete person[k]})

For even older IE just use a for..in loop (with a .hasOwnProperty() check).

And obviously you can put any of the above into a function for ease of re-use:

function emptyObject(obj) {
  Object.keys(obj).forEach(k => delete obj[k])
}

emptyObject(person)

Note that although this answers what you've asked, I'm not sure why you think you need to do it at all. The example you show in the question has the same two properties before and after, so angular.merge() would just overwrite the old values with the new values without any need to first empty the object. (Are you trying to allow for a case (not shown) where the old version of your object might have properties that no longer exist in the new version?)

Solution 2

if you want empty the 'id' only

person['id']=null;

if you want to empty all attributes of the person object, then

Object.keys(person).forEach(key => person[key]=null);

Solution 3

If I understand correctly, you seem to think that you need to empty the original object before merging in the new one or the properties from the database won't be used. I don't think that's the case though. I looked at the source for angular.merge and it appears to copy (ie: overwrite) every property from the source object to the destination object. So you shouldn't need to empty it at all, just do the merge and all the values on the database object will be used.

Share:
14,134
Barun
Author by

Barun

I am a full stack web developer. I have deep interest in programming.

Updated on August 09, 2022

Comments

  • Barun
    Barun almost 2 years

    I have an object like

    var person = {'id':null, 'name':'John Doe'}
    

    After inserting the object value into the database, I will get another object from the server:

    var personInDB = {'id':1234, 'name':'John Doe'}
    

    I have used angular.merge to use updated the value of person with that of personInDB.

    But, I want to empty person object before applying angular.merge, so that I only get the values in database. I don't want to assign new empty object to person as that will break data-binding in angular.

  • nnnnnn
    nnnnnn over 7 years
    When I tried your [person] = [personInDB ] code it had the same result as person = personInDB, it didn't update the existing object that person did refer to (which is what the OP is asking about).
  • RIYAJ KHAN
    RIYAJ KHAN over 7 years
    @nnnnnn can you please tell me how you tested it? It will some additional
  • Barun
    Barun over 7 years
    I want my object to be synced with that of the DB, so if there is problem in the DB code and it doesn't insert name, I want set my local object's name property deleted.
  • nnnnnn
    nnnnnn over 7 years
    jsfiddle.net/x457wb4x - a result that seems consistent with what the MDN page you linked to says, because it's like an even more simplified version of [a,b] = [c,d]...
  • nnnnnn
    nnnnnn over 7 years
    OK, well then the code I've shown should do the emptying you asked for, and I've updated my answer to cover older browsers.