This is my JSON Input:
"data": [
{
"id":3,
"created_by": 1,
"created_at": "2022-01-31T07:00:01.880Z",
},
{
"id":2,
"created_by": 1,
"created_at": "2022-01-31T07:00:01.880Z",
},
{
"id":1,
"created_by": 1,
"created_at": "2022-01-31T07:00:01.880Z",
}
]
I am trying to sort the resulting JSON output by year and month. I have successfully grouped them by year and month separately using the code below, but I'm struggling to combine both groupings into one result. The code and outputs are provided below:
var groupedByYear = _.groupBy(flashMessage,function(item:any) {
return item.created_at.getFullYear();
});
var groupedByMonth = _.groupBy(flashMessage,function(item:any) {
return item.created_at.getMonth();
});
Both functions are returning the results correctly as shown below:
-
"2021": [ { "created_by": 1, "created_at": "2021-01-31T06:54:27.733Z", } ], "2022": [ { "created_by": 1, "created_at": "2022-01-31T06:54:27.733Z", } ],
However, I would like the result to be structured like this:
"2021": [
0:[ {
"created_by": 1,
"created_at": "2021-01-31T06:54:27.733Z",
}]
],
"2022": [
0:[ {
"created_by": 1,
"created_at": "2022-01-31T06:54:27.733Z",
}]
1:[ {
"created_by": 1,
"created_at": "2022-02-31T06:54:27.733Z",
}]
],
Can someone help me achieve this? I am using TypeScript with Node.js and PostgreSQL as my database.