[Vuejs]-Setting tha data array to a initial value after editing that array

0👍

My suggestion is to save the default value somewhere else, such as:

<script>
const defaultArr = [true, false, true, false, true, true, true];

export default {
  data() {
    return {
      arr: [...defaultArr]
    };
  },
  methods: {
    toggleItem(index) {
      this.arr.splice(index, 1, !this.arr[index]);
    },
    resetState() {
      // set the array arr to initial data after some toggleItem() changes
      this.arr = [...defaultArr];
    },
  },
};
</script>

Note the spreading syntax [...] is neccessary since you don’t want to mutate the default array.

Leave a comment