我有以下餐厅订单对象。
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
}
]
}
]
};
如果isDrink的值为true,我想对商品的价格*商品数量求和。 我尝试使用此功能,它将获得所有项目的总数。但是我可以检查isDrink是对还是错,但是我该如何总结饮料价格*数量?
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}
},
请给我一些建议。谢谢!
答案 0 :(得分:1)
您必须:
null
个值中过滤数组isDrink
]中过滤物品
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
}]
}
]
};
function fullTotal(arr) {
if (arr != '') {
return arr.filter(order => order != null).reduce((sum, order) => {
return sum + order.item.filter(item => item.isDrink).reduce((itemSum, item) => (
itemSum + (item.price * item.quantity)
), 0)
}, 0)
} else {
return 0
}
}
console.log(fullTotal(obj.orders));
答案 1 :(得分:0)
尝试
fullTotal: function(arr) {
if (arr!=''){
return arr.reduce((sum, order) => {
return sum + order.item.reduce((itemSum, item) => (
itemSum + item.isDrink ? (item.price * item.quantity) : 0
), 0)
},0)}
else {return 0}
},
答案 2 :(得分:-1)
您可以为数组添加检查,并检查所需的对象和isDrink
。然后返回总和
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 }] }] },
fullTotal = function (array) {
return Array.isArray(array)
? array.reduce((sum, o) => o && Array.isArray(o.item)
? o.item.reduce((s, { price, quantity, isDrink }) => s + (isDrink && price * quantity), sum)
: sum, 0)
: 0;
};
console.log(fullTotal(obj.orders));