I am trying to make the normalized date object. Right now I have
const data = [{
createdDate: '2019-01-06T14:39:33.564Z',
},
{
createdDate: '2018-01-07T14:39:33.564Z',
},
{
createdDate: '2018-01-07T14:39:33.564Z',
},
{
createdDate: '2019-01-06T14:39:33.564Z',
}]
this is the record of when the user is registered on the system. So according to the following i need to make the number of users by year, by month of that year, by week of the selected month of the year I need to sort this array into something like:
const updatedData = [
<year>: [
<month>: [
<week number>: {
count: <number of users registered>
}
]
]
]
So far I tried to make it in this way, but something is missing and cannot figure out what is and how to make it. Please can you explain it, would really appreciate it?
const data = [{
createdDate: '2019-01-06T14:39:33.564Z',
},
{
createdDate: '2018-01-07T14:39:33.564Z',
},
{
createdDate: '2018-01-07T14:39:33.564Z',
},
{
createdDate: '2019-01-06T14:39:33.564Z',
}
]
const updateData = data.map((item, index) => {
const date = new Date(item.createdDate);
const year = date.getFullYear();
const month = date.toLocaleDateString('default', {
month: '2-digit'
});
return {
[year]: [{
[month]: {
userCount: index,
},
}, ],
};
});
console.log(updateData)