I am working on a shopping basket feature using Vue JS, I need to create a subtotal feature which multiplies the price and quantity values of each item and adds them together to create a subtotal.
dataSet: [
{
"instock" : '',
"price": 49,
"qty": 1,
"subTotal": ''
},
{
"instock" : '',
"price": 29,
"qty": 1,
"subTotal" : '',
},
]
I have managed to return the values for each of the 'price' and 'qty' using forEach on the object array like so:
getSubTotal(dataSet) {
this.dataSet.forEach(function(data) {
console.log(data.price)
})
console
49
29
So far so good (I think), I am getting the price values returned to me, the only problem is that they are not being return in the same array but seperately. I really need them to be returned as [49,29] so I am able add the values together for the subtotal. I am able to hard code each array by index then add them seperately but this does not future proof the eventuality of a new clothing item in the object array.
Any advice/criticism or help would be greatly appreciated.
getSubTotal(dataSet) { return this.dataSet.map(function(data) { return data.price; })
Okay so I was able to figure this out.
dataSet: [
{
"price": 49,
"qty": 1,
},
{
"price": 29,
"qty": 1,
},
]
subTotals(data) {
const calcArray = [data.price, data.qty];
return calcArray.reduce((a,b)=> {
//multiply price by quantity for subtotal
return a*b;
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.