Home » Javascript » How can I use a spread operator to spread object

How can I use a spread operator to spread object

Posted by: admin August 19, 2018 Leave a comment

Questions:

I have a function that takes in a state and a property

below is the code that takes the property state and assign it with the property from a form input

const store = new Vuex.Store({
  state: {
    property: {
      address: null,
      bedrooms: 1,
      bathrooms: 1,
      home_type: null,
      home_size: null,
      last_rent: null,
      has_rented: false,
    }
  },
  mutations: {
    updateProperty: function (state, property) {
      Object.assign(state.property, property);
    }
  }
}

Then the code I wrote to do same thing using spread operator but it is not working.

updateProperty: function (state, property) {
  return {...state.property, ...property}
}

I also tried this

updateProperty: function (state, property) {
    state.property = property
}

But it is not working. Any help on how to do it?

Answers: