我有以下类型为Observable<Event[]>:
[{
"_id": 1,
"_title": "Testveranstaltung 1",
"_startDate": "2019-05-29T07:20:00.000Z",
"_endDate": "2019-05-29T08:00:00.000Z",
"_isAllDay": false
}, {
"_id": 2,
"_title": "Testveranstaltung 2",
"_startDate": "2019-06-10T07:00:00.000Z",
"_endDate": "2019-08-02T07:00:00.000Z",
"_isAllDay": false
}, {
"_id": 3,
"_title": "Testveranstaltung 3",
"_startDate": "2019-06-12T07:00:00.000Z",
"_endDate": "2019-06-12T10:00:00.000Z",
"_isAllDay": false
}]
应将数组制成一个二维数组,该数组按startDate月份(moment.js-Object)进行分组。 我想看起来像这样的数组:
[{
"May 2019": [
[
"_id": 1,
"_title": "Testveranstaltung 1",
"_startDate": "2019-05-29T07:20:00.000Z",
"_endDate": "2019-05-29T08:00:00.000Z",
"_isAllDay": false
]
]
},
{
"June 2019": [
[
"_id": 2,
"_title": "Testveranstaltung 2",
"_startDate": "2019-06-10T07:00:00.000Z",
"_endDate": "2019-08-02T07:00:00.000Z",
"_isAllDay": false
],
[
"_id": 3,
"_title": "Testveranstaltung 3",
"_startDate": "2019-06-12T07:00:00.000Z",
"_endDate": "2019-06-12T10:00:00.000Z",
"_isAllDay": false
]
]
}
]
使用此代码,我还没有得到正确的结果:
//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);
});
这是上面的代码输出的内容:
[
[May 2019, June 2019, June 2019],
[
[29/05/2019, 10/06/2019, 12/06/2019]
]
]
谢谢!
答案 0 :(得分:0)
您可以执行以下操作-
const data = [{
"_id": 1,
"_title": "Testveranstaltung 1",
"_startDate": "2019-05-29T07:20:00.000Z",
"_endDate": "2019-05-29T08:00:00.000Z",
"_isAllDay": false
}, {
"_id": 2,
"_title": "Testveranstaltung 2",
"_startDate": "2019-06-10T07:00:00.000Z",
"_endDate": "2019-08-02T07:00:00.000Z",
"_isAllDay": false
}, {
"_id": 3,
"_title": "Testveranstaltung 3",
"_startDate": "2019-06-12T07:00:00.000Z",
"_endDate": "2019-06-12T10:00:00.000Z",
"_isAllDay": false
}];
from(data).pipe(
groupBy(d => d['_startDate'], p => p),
mergeMap(group => zip(of(group.key), group.pipe(toArray())))
).subscribe(console.log);
请参阅以下stackblitz-
https://stackblitz.com/edit/rxjs-groupby-key-vals-hsj3a7?file=index.ts&devtoolsheight=100
您需要根据需要调整日期格式。