F
F
fantazerno2020-09-30 18:11:33
JavaScript
fantazerno, 2020-09-30 18:11:33

How to return an object to its initial state?

Regular object in data in Vue.js

data() {
    return {
      info : {
        state1:10,
        state2:20,
        state3:30,
      }
    }
  }


In the course of work, the values ​​of the properties of the object change. How best to implement the ability to return the info object to its initial state.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Anton, 2020-09-30
@fantazerno

function initialData () {
  return {
    info: {
      state1: 10,
      state2: 20,
      state3: 30
    }
  }
}

export default {
  data () {
    return initialData()
  },
  methods: {
    flush () {
      const data = initialData()
      Object.keys(data).forEach(key => {
        this[key] = initialData[key]
      })
    }
  }
}

V
Vladimir Borutkin, 2020-09-30
@Atanvar

initialState () {
this.info: {
state1: null,
state2: null,
state3: null
}
}

Call the method in the right place.
Nobody bothers to iterate over the object either

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question