hi
i have an array of objects below:
const customers = [
{
"email": "tshepo@umuzi.org",
"status": "OPEN",
"items": [
{
"name": "hamster",
"quantity": 2,
"price": 20
},
{
"name": "saw dust",
"quantity": 1,
"price": 20
},
{
"name": "hamster-cage",
"quantity": 1,
"price": 150
},
{
"name": "book: how to care for your hamster",
"quantity": 1,
"price": 150
}
]
},
{
"email": "tshepo@umuzi.org",
"status": "PAID",
"items": [
{
"name": "balloons",
"quantity": 1,
"price": 25
},
{
"name": "big friggin cake",
"quantity": 1,
"price": 150
},
{
"name": "candles",
"quantity": 1,
"price": 30
}
]
},
{
"email": "tshepo@umuzi.org",
"status": "DELIVERED",
"items": [
{
"name": "tooth brush",
"quantity": 1,
"price": 50
},
{
"name": "soap",
"quantity": 3,
"price": 15
}
]
},
{
"email": "tshepo@umuzi.org",
"status": "DELIVERED",
"items": [
{
"name": "tent",
"quantity": 1,
"price": 1999
}
]
},
{
"email": "sally@umuzi.org",
"status": "DELIVERED",
"items": [
{
"name": "tent",
"quantity": 1,
"price": 1999
},
{
"name": "headlamp",
"quantity": 1,
"price": 250
},
{
"name": "hiking boots",
"quantity": 1,
"price": 1000
}
]
},
{
"email": "sally@umuzi.org",
"status": "PAID",
"items": [
{
"name": "hamster",
"quantity": 1,
"price": 20
},
{
"name": "saw dust",
"quantity": 1,
"price": 20
},
{
"name": "hamster-cage",
"quantity": 1,
"price": 150
}
]
},
{
"email": "sally@umuzi.org",
"status": "DELIVERED",
"items": [
{
"name": "book: how to care for your hamster",
"quantity": 1,
"price": 150
}
]
},
{
"email": "mpho@umuzi.org",
"status": "OPEN",
"items": [
{
"name": "hammer",
"quantity": 1,
"price": 200
},
{
"name": "bag of nails",
"quantity": 1,
"price": 50
},
{
"name": "128 GB SSD Hard drive",
"quantity": 2,
"price": 600
}
]
},
{
"email": "ryan@umuzi.org",
"status": "PAID",
"items": [
{
"name": "128 GB SSD Hard drive",
"quantity": 2,
"price": 600
},
{
"name": "book: how to suceed at being a hard arse",
"quantity": 1,
"price": 160
}
]
},
{
"email" : "mo@umuzi.org",
"status": "DELIVERED",
"items": [
{
"name": "balloons",
"quantity": 1,
"price": 25
},
{
"name": "big friggin cake",
"quantity": 2,
"price": 150
}
]
},
{
"email" : "mo@umuzi.org",
"status": "DELIVERED",
"items": [
]
},
{
"email" : "mo@umuzi.org",
"status": "DELIVERED",
"items": [
{
"name": "balloons",
"quantity": 1,
"price": 25
},
{
"name": "big friggin cake",
"quantity": 1,
"price": 150
}
]
}
]
im struggling to get the solution, here are the instructions below:
//Write a function called required stock that gets all the items that need to be sent out to delivery.
//You need to return data in the correct format. Just include the names and quantities of the items.
//Eg: if one customer paid for 2 hamsters and another customer paid for one hamster and a bag of sawdust
//then your function should return the following data structure but obvously with all names and matching quantitites(added up):
// [
// {"name":"hamster", "quantity": 3},
// {"name": "bag of sawdust", "quantity" 1}
// ]
here is my code below that i have struggled with for about 6 hours already, i can get paid for items and delete the price property but i dont know how to return output in just an array of objects as required in the instructions and i also dont know how to add up all the quantities with matching names:
function requiredStock(customers) {
var allPaidItems = []
for(let customer of customers) {
if(customer.status === "PAID") {
var items = customer.items
items.forEach(i => delete i.price)
allPaidItems.push(items)
}
}
return allPaidItems
}
console.log(requiredStock(customers))
Please help