I currently have an array of objects that looks similar to this where the dates are out of order and each object has the same keys:
const mainArray = [
{Name: John Doe, Date: 1/1/22, Water: 5, Chips: 4, Pizza: 0, HotDog: 0},
{Name: John Doe, Date: 1/5/22, Water: 0, Chips: 0, Pizza: 6, HotDog: 4},
{Name: John Doe, Date: 1/1/22, Water: 5, Chips: 0, Pizza: 0, HotDog: 2},
{Name: Max Dow, Date: 2/7/22, Water: 5, Soda: 4, Pizza: 0, HotDog: 0},
{Name: Max Dow, Date: 2/7/22, Water: 0, Soda: 4, Pizza: 2, HotDog: 1},
{Name: Max Dow, Date: 1/5/22, Water: 1, Soda: 1, Pizza: 0, HotDog: 0},
]
And I would like to combine objects if they share the same name and date. When combining those objects I am also trying to sum like key values together.
The result would look like this:
const mainArray = [
{Name: John Doe, Date: 1/1/22, Water: 10, Chips: 4, Pizza: 0, HotDog: 2},
{Name: John Doe, Date: 1/5/22, Water: 0, Chips: 0, Pizza: 6, HotDog: 4},
{Name: Max Dow, Date: 2/7/22, Water: 5, Soda: 8, Pizza: 2, HotDog: 1},
{Name: Max Dow, Date: 1/5/22, Water: 1, Soda: 1, Pizza: 0, HotDog: 0},
]
I was able to build a function that worked only if the object name and date were in order next to each other however I was unable to find a way to organize all objects by both name and date.
Thank you for your time and effort!