I have an array of objects for a podcast app that looks like this:
[{ id: "uuid-1"
timeInSeconds: 1000
dateListened: "2021-01-01T15:57:17.000Z" }, // <---same day
{ id: "uuid-2"
timeInSeconds: 4900
dateListened: "2021-01-01T16:57:17.000Z" }, // <---same day
{ id: "uuid-3"
timeInSeconds: 3200
dateListened: "2021-09-01T16:57:17.000Z" },
{ id: "uuid-4"
timeInSeconds: 6000
dateListened: "2021-10-01T16:57:17.000Z" } ]
I want to create a function that combines activity times if the dateListened is on the same day. I want it to look something like this:
[{ id: "uuid-new"
timeInSeconds: 5900 // <---- combined seconds listened on Jan 1
dateListened: "2021-01-01T15:57:17.000Z" },
{ id: "uuid-3"
timeInSeconds: 3200
dateListened: "2021-09-01T16:57:17.000Z" },
{ id: "uuid-4"
timeInSeconds: 6000
dateListened: "2021-10-01T16:57:17.000Z" } ]
My closest attempts have been to use a .map() with a nested .some() but I'm still far off enough that it's not even worth posting my tries. Does anyone have any hints or ideas of what direction to try next? Thank you!
isSameDay(date1, date2)mapwont work here because it always returns the same number of elements as is input. To change the number of outputs, you would generally usereduce.