I am working with entries of type Observable<Event[]>
, and they are structured as follows:
[{
"_id": 1,
"_title": "Test Event 1",
"_startDate": "2019-05-29T07:20:00.000Z",
"_endDate": "2019-05-29T08:00:00.000Z",
"_isAllDay": false
}, {
"_id": 2,
"_title": "Test Event 2",
"_startDate": "2019-06-10T07:00:00.000Z",
"_endDate": "2019-08-02T07:00:00.000Z",
"_isAllDay": false
}, {
"_id": 3,
"_title": "Test Event 3",
"_startDate": "2019-06-12T07:00:00.000Z",
"_endDate": "2019-06-12T10:00:00.000Z",
"_isAllDay": false
}]
I need to transform this array into a two-dimensional array grouped by the month of the start date (using moment.js Objects). The desired format is as follows:
[{
"May 2019": [
[
"_id": 1,
"_title": "Test Event 1",
"_startDate": "2019-05-29T07:20:00.000Z",
"_endDate": "2019-05-29T08:00:00.000Z",
"_isAllDay": false
]
]
},
{
"June 2019": [
[
"_id": 2,
"_title": "Test Event 2",
"_startDate": "2019-06-10T07:00:00.000Z",
"_endDate": "2019-08-02T07:00:00.000Z",
"_isAllDay": false
],
[
"_id": 3,
"_title": "Test Event 3",
"_startDate": "2019-06-12T07:00:00.000Z",
"_endDate": "2019-06-12T10:00:00.000Z",
"_isAllDay": false
]
]
}
]
The code I have tried so far is not giving me the expected output:
// Observable<Event[]> is returned by a function
.pipe(groupBy((events: Event[]) => events.map((event: Event) => event.startDate.format('MMMM YYYY')), (events: Event[]) => events.map((event: Event) => event.startDate.format('DD/MM/YYYY'))),
mergeMap(group => zip(of(group.key), group.pipe(toArray())))
.subscribe((data) => {
console.log(data);
});
This is the current output from the code mentioned above:
[
[May 2019, June 2019, June 2019],
[
[29/05/2019, 10/06/2019, 12/06/2019]
]
]
Any insight on how to achieve the desired grouping would be much appreciated. Thank you!