vue删除数组对象中的一个属性
可以使用Vue.js的$delete方法来删除数组对象中的一个属性。
示例代码:
data() {
return {
items: [
{ id: 1, name: 'item 1', quantity: 10 },
{ id: 2, name: 'item 2', quantity: 5 },
{ id: 3, name: 'item 3', quantity: 3 },
]
}
},
methods: {
removeQuantity(index) {
this.$delete(this.items[index], 'quantity');
}
}
在上面的示例中,我们定义了一个items数组,它包含三个对象,每个对象有id、name和quantity属性。我们也定义了一个removeQuantity方法,它接收一个index参数,该参数表示要删除哪个对象的quantity属性。在方法中,我们使用$delete方法来删除items数组中指定索引处对象的quantity属性。
原文地址: https://www.cveoy.top/t/topic/bGBm 著作权归作者所有。请勿转载和采集!