I wanted to get the total productPrice
from the chosenData
array. I need to compare it from the mainData
and only get its total value. My problem is that I'm getting undefined and how should I do it and is there a shorter way of doing it?
const mainData = [
{
"id": "4324",
"productName": "Apple",
"productPrice": 250,
},
{
"id": "656546b",
"productName": "Banana",
"productPrice": 500,
},
{
"id": "5656",
"productName": "Soup",
"productPrice": 800,
}
];
const chosenData = ['5656', '656546b']
const newArray = chosenData.map((value, index) => ({
productPrice: mainData.find((data) => data.id === value)
}))
console.log(newArray.productPrice)