Reputation: 229
I am trying to calculate the "total cost" of the presents Array in "Object: presents" for a given name (in this case "Peter) by finding the price of the presents in "Object: prices". If the present is not in the Prices array, the price would be 0.
ie: In the case of Peter the "total cost" of the presents would be 1,001 and in the case of Dana would be 6,800.
The outcome would be an array of prices given the matched presents within the 2 arrays of objects (ie for peter = [1,1000], since his presents are "coffee" and "holidays") so later I calculate the sum inside of it with my reducer function.
I have tried to get the array by filtering the name and then try to find the elements included but my solution only iterates on the first element (coffee) and it returns an undefined array.
Any ideas on why and hints on how to move forward?
I am a beginner coder and would really appreciate some guidance.
Cheers,
const presents = [
{ "name": "peter", "presents": ["coffee", "holidays"], "present": "sock", "score": 10 },
{ "name": "dana", "presents": ["car", "phone"], "present": "sock", "score": 9.25 }
]
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 name = 'peter'
const presentsPrices = []
const filteredName = presents.filter((element)=>element.name===name?element:false).map((element, i)=> element.presents[i].includes(prices[i].present)? presentsPrices.push(prices[i].price):0)
const reducer = (accumulator, currentValue) => accumulator + currentValue;
const sum = presentsPrices.reduce(reducer)
console.log(sum)
Upvotes: 1
Views: 265
Reputation: 28434
You can use .find
to get the price of a present. Also, there's no need for .filter
and .map
since you're searching for one object and then get its presents
if exists:
const presents = [
{ "name": "peter", "presents": ["coffee", "holidays"], "present": "sock", "score": 10 },
{ "name": "dana", "presents": ["car", "phone"], "present": "sock", "score": 9.25 }
];
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 name = 'peter';
// get presents of person with this name
const { presents: filteredNamePresents = [] } = presents.find(element => element.name===name) || {};
console.log(`Presents: ${filteredNamePresents}`);
// get prices list of these presents
const presentsPrices = filteredNamePresents.reduce((acc,item) => {
const { price } = prices.find(price => price.present===item) || {};
acc = price ? [...acc, price] : acc;
return acc;
}, []);
console.log(`Prices: ${presentsPrices}`);
// calculate totla sum of the prices
const sum = presentsPrices.reduce((accumulator, currentValue) => accumulator + currentValue);
console.log(`Sum: ${sum}`);
Upvotes: 1
Reputation:
Here's something that can help your code:
const people = {
"peter": ["coffee", "holidays"],
...
};
const presents = {
"coffee": 1,
"holidays": 1000,
...
};
But otherwise than better organization with JSON I can't really help you with your problem.
Upvotes: 0