I have the following data that I want to sort based on the date - not including the timestamp.
NOTE: I have access to moment for this task.
My data looks like the following:
const data = [
{
"fixture": "AC v Inter",
"kickOffTime": "2018-06-14T15:00:00Z",
},
{
"fixture": "DC v NYC",
"kickOffTime": "2018-06-15T12:00:00Z",
},
{
"fixture": "AFC v LPC",
"kickOffTime": "2018-06-15T15:00:00Z",
},
{
"fixture": "DTA v MC",
"kickOffTime": "2018-06-15T18:00:00Z",
},
{
"fixture": "LAC v GC",
"kickOffTime": "2018-06-16T18:00:00Z",
}
];
I have tried a number of approaches. The final result I am hoping to achieve is the following data structure.
const updatedDataStructure = [
{
date: "2018-06-14",
fixtures: [{
"fixture": "AC v Inter",
"kickOffTime": "2018-06-14T15:00:00Z",
}]
},
{
date: "2018-06-15",
fixtures: [
{
"fixture": "DC v NYC",
"kickOffTime": "2018-06-15T12:00:00Z",
},
{
"fixture": "AFC v LPC",
"kickOffTime": "2018-06-15T15:00:00Z",
},
{
"fixture": "DTA v MC",
"kickOffTime": "2018-06-15T18:00:00Z",
},
]
},
{
date: "2018-06-16",
fixtures: [{
"fixture": "LAC v GC",
"kickOffTime": "2018-06-16T18:00:00Z",
}]
},
];
Here was my latest attempt which NEARLY worked:
const result = fixtures.reduce(function (r, a) {
r[moment(a.kickOffTime).format('ddd Do MMM')] = r[moment(a.kickOffTime).format('ddd Do MMM')] || [];
r[moment(a.kickOffTime).format('ddd Do MMM')].push(a);
return r;
}, Object.create(null));