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:
let mainArray = [
{Name: 'John Doe', Date: '2022-1-1', 'Water': 5, 'Chips': 4,'Pizza': 0, 'HotDog': 0},
{Name: 'John Doe', Date: '2022-1-1', 'Water': 2, 'Chips': 0,'Pizza': 1, 'HotDog': 1},
{Name: 'John Doe', Date: '2022-1-5', 'Water': 0, 'Chips': 0, 'Pizza': 6, 'HotDog': 4},
{Name: 'John Doe', Date: '2022-1-1', 'Water': 5, 'Chips': 0, 'Pizza': 0, 'HotDog': 2},
{Name: 'Max Dow', Date: '2022-2-7', 'Water': 5, 'Soda': 4, 'Pizza': 0, 'HotDog': 0},
{Name: 'Max Dow', Date: '2022-2-7', 'Water': 0, 'Soda': 4, 'Pizza': 2, 'HotDog': 1},
{Name: 'Max Dow', Date: '2022-1-1', '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:
let mainArray = [
{Name: 'John Doe', Date: '2022-1-1', 'Water': 12, 'Chips': 4, 'Pizza': 1, 'HotDog': 3},
{Name: 'John Doe', Date: '2022-1-5', 'Water': 0, 'Chips': 0, 'Pizza': 6, 'HotDog': 4},
{Name: 'Max Dow', Date: '2022-2-7', 'Water': 5, 'Soda': 8, 'Pizza': 2, 'HotDog': 1},
{Name: 'Max Dow', Date: '2022-1-1', '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.
let newObjectArray = []
let currentObject = {}
let currentName = ""
let currentDate = ""
for (let i = 0; i < mainArray.length; i++) {
if (i === 0) {
currentObject = mainArray[0]
currentName = mainArray[0]["Name"]
currentDate = mainArray[0]["Date"]
} else if (
mainArray[i]["Name"] === currentName &&
mainArray[i]["Date"] === currentDate
) {
//ensures name and date do not try to be summed
for (const item in mainArray[i]) {
if (typeof mainArray[i][item] === "number") {
//adds item value into the current object's item
currentObject[item] += mainArray[i][item]
}
}
} else {
//places completed object in array and places new values to check against
newObjectArray.push(currentObject)
currentObject = mainArray[i]
currentName = mainArray[i]["Name"]
currentDate = mainArray[i]["Date"]
}
//Makes sure the last Object is pushed
if (i + 1 === mainArray.length) {
newObjectArray.push(currentObject)
}
}
My current function gets me close but the result looks like this where only the values next to each other combine.
let newObjectsArray = [
{Name: 'John Doe', Date: '2022-1-1', 'Water': 7, 'Chips': 4, 'Pizza': 1, 'HotDog': 1},
{Name: 'John Doe', Date: '2022-1-5', 'Water': 0, 'Chips': 0, 'Pizza': 6, 'HotDog': 4},
{Name: 'John Doe', Date: '2022-1-1', 'Water': 5, 'Chips': 0, 'Pizza': 0, 'HotDog': 2},
{Name: 'Max Dow', Date: '2022-2-7', 'Water': 5, 'Soda': 8, 'Pizza': 2, 'HotDog': 1},
{Name: 'Max Dow', Date: '2022-1-1', 'Water': 1, 'Soda': 1, 'Pizza': 0, 'HotDog': 0},
]
If there is a proper way to combine such objects that would be ideal. I would also be able to solve this problem if I could sort the objects by name/date.
Thank you for your time and effort!