I am currently working with an object that contains orders from a restaurant.
var obj = {
orders: [
null,
{
date: "2018-07-09 10:07:18",
orderVerified : true,
item: [
{
name: "apple juice",
price: 3.9,
quantity: 1,
isDrink: true
},
{
name: "Hawaii pizza",
price: 7,
quantity: 2,
isDrink: false
}
]
},
{
date: "2018-07-09 10:07:30",
orderVerified : false,
item: [
{
name: "Warmer Topfenstrudel",
price: 3.9,
quantity: 1,
isDrink: false
}
]
},
{
date: "2018-07-09 15:07:18",
orderVerified : true,
item: [
{
name: "Coca Cola 2 l",
price: 12.9,
quantity: 3,
isDrink:true
}
]
},
{
date: "2018-06-13 10:07:18",
orderVerified : true,
item: [
{
name: "Wiener Schnitzel vom Schwein",
price: 9.9,
quantity: 2,
isDrink: false
}
]
}
]
};
I need to calculate the total price of all drinks in the orders by multiplying their individual prices and quantities when isDrink is true. Although I have tried using the function provided below to sum up the total items cost, I faced difficulties distinguishing between drinks and non-drinks in order to compute the final amount.
fullTotal: function(arr) {
if (arr!=''){
return arr.reduce((sum, order) => {
return sum + order.item.reduce((itemSum, item) => (
itemSum + (item.price * item.quantity)
), 0)
},0)}
else {return 0}
},
I would appreciate any suggestions or guidance on how to approach this issue. Thank you!