Conditional selecting in function

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP

Conditional selecting in function



I have the following object with orders of 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 want to summing up the item's prices * items quantities if the value of isDrink is true.
I've tried to use this function, which get the total of all items. but I can check check wether isDrink is true or false, but how could I summing up the drinks prices * quantities?


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
,



Please give me some suggestion with this. Thank you!




3 Answers
3



Try it. You had to filter array from null values in your case.


null




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.isDrink ? (item.price * item.quantity) : 0
), 0)
, 0)
else
return 0



console.log(fullTotal(obj.orders));



Try this


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
,



You could add a check for an array and check the needed object and isDrink. Then return the sum


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));






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

Executable numpy error

Trying to Print Gridster Items to PDF without overlapping contents

Mass disable jenkins jobs