mafortis
mafortis

Reputation: 7128

vuejs remove item from array

I have a form value which gets id's each time i save data, but if i delete any of those data the id of it won't be deleted from form value.

data() {
  return {
    savedVariations: [],
    form: {
      variations: [],
    }
  }
},
methods: {
  addVariants(e) {
    e.preventDefault();
    axios.post('/api/admin/variations/store', {
      childs: this.variationChilds,
      parent: this.variationParents,
    })
    .then(res => {
      this.form.variations.push(res.data.data.id); // send id to form.variations
    })
  },
  removeSavedParent(id, index){
    axios.delete('/api/admin/variations/destroy/'+id).then((res) => {
      this.form.variations.splice(id); // delete the id from form.variations (not working)
      this.savedVariations.splice(index, 1);
    })
  },
}

When i save new item my form.variations becomes variations["1","50", "30"] but when i delete any of those items the id won't be removed from form.variations

Any idea how to remove id from form variable when i delete the data?

Upvotes: 2

Views: 1629

Answers (1)

Qonvex620
Qonvex620

Reputation: 3972

You have to find the index of id in your array and remove it, see code below

this.form.variations.splice(this.form.variations.indexOf(id), 1);

Upvotes: 2

Related Questions