How can I trigger an update when altering part of an object found by index in an array?
The docs show how to alter the value of an array:
You could update the sub-property in the array element with this.$set(). For example, to increment an x
subproperty in the first two array elements (creating the sub-property if it doesn't exist):
methods: {
update() {
this.$set(this.arr[0].foo, 'x', (this.arr[0].foo.x || 0) + 100)
this.$set(this.arr[1].foo, 'x', (this.arr[1].foo.x || 0) + 100)
}
}
new Vue({
el: '#app',
data() {
return {
arr: [
{
foo: {
x: 100,
y: 200
}
},
{
foo: {
/* x does not exist here initially */
y: 400
}
}
]
};
},
methods: {
update() {
this.$set(this.arr[0].foo, 'x', (this.arr[0].foo.x || 0) + 100)
this.$set(this.arr[1].foo, 'x', (this.arr[1].foo.x || 0) + 100)
}
}
})
arr[0]: {{ arr[0] }}
arr[1]: {{ arr[1] }}
codepen