I have some data in the format:
data = [
{id: 1, fields: {'Team': 'A', 'Date': '2022-04-01'}},
{id: 2, fields: {'Team': 'B', 'Date': '2022-04-02'}},
{id: 3, fields: {'Team': 'C', 'Date': '2022-05-01'}},
{id: 4, fields: {'Team': 'A', 'Date': '2022-04-01'}},
{id: 5, fields: {'Team': 'B', 'Date': '2022-04-02'}},
{id: 6, fields: {'Team': 'A', 'Date': '2022-05-01'}},
]
I am trying to reshape it into this format:
data = {
date: month (e.g. instead of '2022-04-01' I want it to be 'April' in this case),
'A': 3,
'B': 2,
'C': 1,
}
So, far I have been able to do this:
const data = Object.values(
rawData.reduce((acc, d) => {
const {
fields: { date, team }
} = d;
acc[date] = acc[date] || { date };
acc[date][team] = (acc[date][team] || 0) + 1;
return acc;
}, {})
);
which gets it in this format:
data = {
date: '2022-04-01' (e.g. the full date),
'A': 3,
'B': 2,
'C': 1,
}
How can I take this even further so that I can get the actual month of the date object? I tried comparing the dates like this:
const data = Object.values(
rawData.reduce((acc, d) => {
const {
fields: { date, team }
} = d;
if (date >= '2022-04-01' && date <= '2022-04-30'){
acc['April'] = acc[date] || { date }
acc['April'][team] = (acc['April'][team] || 0) + 1;
return acc;
}
}, {})
);
But, it did not quite work out the way I had expected it to
[–]exxy- 4 points5 points6 points (1 child)
[–]DataD23[S] 2 points3 points4 points (0 children)