[Vuejs]-Problem with checking ID in filter function inside a method

0👍

You can use watch function to subscribe when goodToUpdate object was updated and call updateGood function after that.

// your main component

{
  methods: {
  updateGood(updatedGood){
      // Creating new array with only one object inside 
      let goodOriginalIndex = this.goods.indexOf(goodOriginalObject[0]);
      this.goods.splice(goodOriginalIndex, 1, updatedGood);
      alert("Js"+JSON.stringify(updatedGood));

      this.goodToUpdate = null // reset after update

      // if you just want to replace the old good by the new good, you can use map
      this.goods = this.goods.map(good => good.id === updatedGood.id ? updatedGood : good )
    },
    
   ...
  },
  watch: {
    // whenever question changes, this function will run
    goodToUpdate: function (newGood, oldGood) {
      if(newGood){
        this.updateGood(newGood)
      }
      
    }
  },
}

Leave a comment