2

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?

2
  • 1
    Array.prototype.splice needs to know how many elements to delete. "this.form.variations.splice(id, 1)" might work. (See 'developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…). Commented Jan 21, 2020 at 7:09
  • @Cat that didn't work Commented Jan 21, 2020 at 7:14

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.