I have a dataset like this:
[
{
"Happy": 1,
"createdAt": "2021-12-20"
},
{
"Tired": 1,
"createdAt": "2021-12-14"
},
{
"Energetic": 1,
"createdAt": "2021-12-13",
"Anxious": 1
},
{
"Energetic": 1,
"createdAt": "2021-12-13",
"Anxious": 1
},
{
"Tired": 1,
"createdAt": "2021-12-09",
"Anxious": 1
},
{
"Tired": 1,
"createdAt": "2021-12-09",
"Anxious": 1
},
{
"Sad": 1,
"createdAt": "2021-12-09"
},
{
"Sad": 1,
"createdAt": "2021-12-08"
},
{
"Happy": 1,
"createdAt": "2021-12-08"
},
{
"Anxious": 1,
"createdAt": "2021-12-08"
},
{
"Happy": 1,
"createdAt": "2021-12-08"
},
{
"Tired": 1,
"createdAt": "2021-12-08"
},
{
"Energetic": 1,
"createdAt": "2021-12-08"
},
{
"Anxious": 1,
"createdAt": "2021-12-08"
},
{
"Happy": 1,
"createdAt": "2021-12-08"
},
{
"Sad": 1,
"createdAt": "2021-12-08"
},
{
"Happy": 1,
"createdAt": "2021-12-08"
},
{
"Happy": 1,
"createdAt": "2021-12-08"
},
{
"Anxious": 1,
"createdAt": "2021-12-08"
},
{
"Sad": 1,
"createdAt": "2021-12-08"
},
{
"Anxious": 1,
"createdAt": "2021-12-08"
},
{
"Anxious": 1,
"createdAt": "2021-12-08"
},
{
"Tired": 1,
"createdAt": "2021-12-08"
},
{
"Anxious": 1,
"createdAt": "2021-12-08"
},
{
"Anxious": 1,
"createdAt": "2021-12-08"
}
]
What I want to do is group this by createdAt and if there are duplicates keys within said date, add the duplicates together. For example:
{
"Energetic": 1,
"createdAt": "2021-12-13",
"Anxious": 1
},
{
"Energetic": 1,
"createdAt": "2021-12-13",
"Anxious": 1
},
will be
{
'Tired': 2,
'Anxious': 2,
'createdAt': '2021-12-13'
}
Another example:
{
"Sad": 1,
"createdAt": "2021-12-08"
},
{
"Happy": 1,
"createdAt": "2021-12-08"
},
{
"Anxious": 1,
"createdAt": "2021-12-08"
},
{
"Happy": 1,
"createdAt": "2021-12-08"
},
{
"Tired": 1,
"createdAt": "2021-12-08"
},
{
"Energetic": 1,
"createdAt": "2021-12-08"
},
{
"Anxious": 1,
"createdAt": "2021-12-08"
},
{
"Happy": 1,
"createdAt": "2021-12-08"
},
{
"Sad": 1,
"createdAt": "2021-12-08"
},
{
"Happy": 1,
"createdAt": "2021-12-08"
},
{
"Happy": 1,
"createdAt": "2021-12-08"
},
{
"Anxious": 1,
"createdAt": "2021-12-08"
},
{
"Sad": 1,
"createdAt": "2021-12-08"
},
{
"Anxious": 1,
"createdAt": "2021-12-08"
},
{
"Anxious": 1,
"createdAt": "2021-12-08"
},
{
"Tired": 1,
"createdAt": "2021-12-08"
},
{
"Anxious": 1,
"createdAt": "2021-12-08"
},
{
"Anxious": 1,
"createdAt": "2021-12-08"
}
will look like
{
'Sad': 3,
'Anxious': 7,
'Happy': 5,
'Tired': 2,
'Energetic': 1,
'createdAt': '2021-12-08',
}
I was able to get the dataset like this so far:
response.value.reduce((acc, val) => {
acc[toMoodMap(val) as string] = acc[val] === undefined ? 1 : (acc[val] += 1);
acc['createdAt'] = assessment.createdAt;
moodArr.push(acc);
return acc;
}, {});
I'm aware of lodash's groupBy, but it's currently not available to me unfortunately. Is there a way to achieve the end result?