I want to add a field inside each presentsData object called sumP and fill it with the presents item sum.
Second goal, if sumP > money I would like to delete the most expensive item inside It's not working
const presentsData= [
{
name: "Peter",
presents: ["coffee","holidays"],
money: 7000
},
{
name: "Mario",
presents: ["car","coal"],
money: 300
},
{
name: "Amanda",
presents: ["computer","coal"],
money: 300
},
{
name: "David",
presents: ["clothes", "car"],
money: 2000
}
]
const prices= [
{
present: "coffee",
price: 1
},
{
present: "holidays",
price: 1000
},
{
present: "videogames",
price: 40
},
{
present: "computer",
price: 600
},
{
present: "tattoo",
price: 30
},
{
present: "clothes",
price: 80
},
{
present: "car",
price: 6000
},
{
present: "phone",
price: 800
},
{
present: "motorbike",
price: 3500
},
{
present: "coal",
price: 0
}
]
const pricesMap = new Map(prices.map(({ present, price }) => [present, price]))
const res1 = presentsData.map(s=>{
return {...s,
sumP: s.presents.reduce((acc, p) => acc + pricesMap.get(p),0)
}
})
console.log("this is res1=>",res1) //this is presentsData result after adding the field sumP
console.log(pricesMap)
const res2 = res1.map((r)=>{
if(r.sumP > r.money){
( /* would like to delete the item inside "presents" with the bigger price using pricesMap */)
}
})
console.log(res2)
pd: what I find hard is to find out how to iterate in presents , array inside object inside array.