Adding and removing objects and arrays in vuejs

Objects

  • Set: Vue.set(object,value)
  • Unset: Vue.delete(Object,key)

    data() {
         return {
            tags: {'tag1','tag2'}
        }
    }
    ...
    
     methods: {
        add: function(tag) {
            Vue.set(this.tags,tag);
        },
        remove: function(id) {
            Vue.delete(this.tags,key);
        }
    }

Arrays

  • Set: this.tags.push(tag)
  • Delete: this.tags.splice(this.tags.indexOf(key), 1);

    data() {
         return {
            tags: ['tag1','tag2']
        }
    }
    ...
    
     methods: {
        add: function(tag) {
            this.tags.push(tag);
        },
        remove: function(id) {
            Vue.delete(this.tags,key);
        }
    }