I have an empty products array and users can add new products if they want. My goal is I want to save the last productKey and save it in a Vuex state.
//data
products: []
//method
addNewProduct() {
this.products.push({
productKey: '',
productName: ''
})
},
I want to get the key of the last added product (last object). I've tried this way
lastKey() {
return this.products[this.products.length-1].productKey
}
This doesn't work as it says cannot read properties of undefined (reading 'productKey'). Any help would be greatly appreciated. thank you.