I wrote this simple filter array per data time, but not sure if it's good way.
Example data:
[
{
"content": "test1",
"createdAt": "2020-05-29T13:46:14.000Z"
},
{
"content": "test2",
"createdAt": "2020-05-29T13:46:14.000Z"
},
{
"content": "test3",
"createdAt": "2020-05-27T13:46:14.000Z"
}
]
And i want to convert it like this:
{
"Fri May 29 2020": [
{
"content": "test1",
"createdAt": "2020-05-29T13:46:14.000Z"
},
{
"content": "test2",
"createdAt": "2020-05-29T13:46:14.000Z"
}
],
"Wed May 27 2020": [
{
"content": "test3",
"createdAt": "2020-05-27T13:46:14.000Z"
}
]
}
And there is my code:
let usedDates = new Set();
let logs = {};
for(let log of data) {
const date = new Date(log.createdAt).toDateString();
if(usedDates.has(date)) continue;
logs[date] = data.filter((e) => date == new Date(e.createdAt).toDateString());
usedDates.add(date);
}
yeah it works, but not sure if it's good way.