This is my Input json
"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 want to sort a resultent json out put with year and month means each year it has to grouped by each month for that i tried the below code it is working fine for year alone and month alone but i am not sure how can i combaine both together i tried calling groupedByYear inside groupedByMonth But it is not givinng me output rather it is throwing Undefined out put the code and the outputs are given below. And the result which i am looking for as well.
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 are returning results correctly. as follows. 1)
"2021": [
{
"created_by": 1,
"created_at": "2021-01-31T06:54:27.733Z",
}
],
"2022": [
{
"created_by": 1,
"created_at": "2022-01-31T06:54:27.733Z",
}
],
-
"0": [ { "created_by": 1, "created_at": "2021-01-31T06:54:27.733Z", } ], "1": [ { "created_by": 1, "created_at": "2022-02-31T06:54:27.733Z", } ],
But i want a result somthing 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",
}]
],
How can I do this? I am using type script with nodejs and my db is postgres. Is there some one who can help me with this?