I am confused with array inside object inside array, want to add a field inside presentsData call it sumP and inside get the sum of the value inside array presents using the array prices.
I tried reduce but it wasn't working, I tried with map and find and I am not getting a number I am getting an object, and can't figure out how to loop the both values
This is my code:
let 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
}
]
const res = presentsData.map(s =>
({
...s,
sumP: prices.find(e=>e.present === s.presents[0] ? e.price: 0)
}));
console.log(res)
this is what I expected:
[
{
name: "Peter",
presents: ["coffee","holidays"],
money: 7000
sumP: (/*SUM OF PRESENTS'S VALUE*/)
},
{
name: "Mario",
presents: ["car","coal"],
money: 300
sumP: (/*SUM OF PRESENTS'S VALUE*/)
},
{
name: "Amanda",
presents: ["computer","coal"],
money: 300
sumP: (/*SUM OF PRESENTS'S VALUE*/)
},
{
name: "David",
presents: ["clothes", "car"],
money: 2000
sumP: (/*SUM OF PRESENTS'S VALUE*/)
}
]