Abhijit Mondal Abhi
Abhijit Mondal Abhi

Reputation: 1829

How to append properties in an object in Vuejs

I have Data in vuejs like following:

params: {
        comment: Object,
        state: "",
        deleteAppointment: false,
        appointmentId: null,
      }

I am filling data from two functions. First function is just assigning following lines:

  this.params.state = "BLACKLIST";
        this.params.deleteAppointment = true;
        this.params.appointmentId = this.appointmentId;

But, in the second function when I am assigning following:

   const comment = {};
      fd.forEach(function(value, key){
        comment[key] = value;
      });

      const data = {};
      Object.keys(this.lead).map((key) => {
        if (this.lead[key] != this.orginal[key]) {
          data[key] = this.lead[key];
        }
      });

      this.params = data; // May be the problem is here, data is overwriting existing properties of params
      this.params.comment = comment;

When assigning data in params, previous properties are vanishing! May be I need object copy or something! I couldn't understand what I have to do actually right now.

Upvotes: 0

Views: 438

Answers (1)

doğukan
doğukan

Reputation: 27401

You should inherit the previous object using Spread Operator, try this

this.params = {...this.params, ...data};

Upvotes: 3

Related Questions