在用 Vue-watch 观察的一组对象中,如何获取已更改对象的索引?

In an array of obects watched with Vue-watch, how to get index of object that was changed?

我有一个对象数组,如下所示:

 myArray: [{
 name: "First",
 price: 10,
 rebate: 5,
 listPrice: 15,
 outcome: 0
 },{
 name: "Second",
 price: 11,
 rebate: 5,
 listPrice: 16,
 outcome: 0
}

我想在同一对象中的任何其他值发生变化时重新计算 outcome 值。

我已经有了这样的设置,但它会查找任何对象的变化,然后重新计算整个数组。我已经设法通过组合使用 computedwatch 函数来设置它。然而,他们观察整个数组的变化,然后重新计算数组中所有对象的结果值。

我如何观察变化然后重新计算仅变化的对象

下面是我当前用于重新计算整个数组的函数(观看另一个 属性),但我正在寻找的可能完全不同。

计算:

myArrayWasChanged() {
return [this.myArray.reduce((a, {vendors}) => a + vendors, 0), this.myArray.filter(item => item.discounted == false).length] 

观看:

myArrayWasChanged: {
handler: function (val, oldVal) {
this.recalculateIsVendor();

鉴于 outcome 完全依赖于其他属性,它实际上并不是组件状态的一部分。因此,在组件的 data 中,您可以存储没有 outcome 的数组,然后使用结果 计算数组的新版本 作为计算的 属性.

data: function () {
  return {
    myArrayWithoutOutcome: [
    {
      name: "First",
      price: 10,
      rebate: 5,
      listPrice: 15
    },
    {
      name: "Second",
      price: 11,
      rebate: 5,
      listPrice: 16
    }]
  }
},
computed: {
  myArrayWithOutcome: function () {
    return this.myArrayWithoutOutcome.map(x => {
      return {...x, outcome: this.calculateOutcome(x)}
    })
  }
},
methods: {
  calculateOutcome(item) {
    // Logic to calculate outcome from item goes here
    return 0
  }
}