I have an array of objects, data, that I'm attempting to retrieve the total of both shoe sizes.
I'm having issues counting "size 10" apart from "size 5". I've attempted to use the .reduce() method with a conditional to only add the total count if the names match:
const data = [
{ "shoeSize": "size 10", "total": 9 },
{ "shoeSize": "size 10", "total": 3 },
{ "shoeSize": "size 5", "total": 2 }
];
const grandTotal = data.reduce((prev, curr) => {
return {
shoeSize10Total:
prev.shoeSize === "size 10" ||
curr.shoeSize === "size 10" &&
prev.total + curr.total,
shoeSize5Total:
prev.shoeSize === "size 5" ||
curr.shoeSize === "size 5" &&
prev.total + curr.total
};
});
console.log(grandTotal);
However, the result of this method returns NaN and false - instead of the total amount of numbers for each.
I've tried looking up a solution and found something similar on this post, but they are filtering to return the value of 1; I would like to return both in an object.
This is the intended result of my .reduce():
const grandTotal = {
shoeSize10Total: 12
shoeSize5Total: 2
};