最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to empty an JS Object? - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question asked Sep 5, 2016 at 5:00 BarunBarun 4,4314 gold badges24 silver badges30 bronze badges 1
  • Why you not try to use $resource then? – RIYAJ KHAN Commented Sep 5, 2016 at 5:35
Add a comment  | 

5 Answers 5

Reset to default 7

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?)

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.

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);

Well, to avoid breaking angularjs's data binding I now update each field manually:

var person = {id : null, name: 'toto'};

// Ajax call
person.id = ajaxres.id;
person.name = ajaxres.name

If you are using ECAM6,then you can use Destructuring_assignment

[person] = [personInDB ] //Object {id: 1234, name: "John Doe"}

OR

You can have like this.

var arrKeys = Object.keys(person);

  arrKeys.forEach(function(val,key){  

      person[val] =   personInDB[val];

    })
发布评论

评论列表(0)

  1. 暂无评论