I am currently working on refining a shopping basket function with Vue JS. One of the key features I need to implement is a subtotal calculation that multiplies the price and quantity values for each item and then combines them to create an overall subtotal.
dataSet: [
{
"instock" : '',
"price": 49,
"qty": 1,
"subTotal": ''
},
{
"instock" : '',
"price": 29,
"qty": 1,
"subTotal" : '',
},
]
Initially, I was able to retrieve the 'price' and 'qty' values using a forEach loop in the object array like this:
getSubTotal(dataSet) {
this.dataSet.forEach(function(data) {
console.log(data.price)
})
console
49
29
While I successfully retrieved the price values, they were returned individually rather than as part of an array. The challenge I am facing now is how to return them as [49, 29] so that I can easily add them together to calculate the total. Hard-coding by index or adding them separately is not ideal for scalability, especially when new clothing items are added to the object array.
Any suggestions, feedback, or assistance would be greatly appreciated!