我有entries$: Observable<BooksOverviewGroup[]>;
:
我想按 groupId 对它们进行分组。我试过这样:
groupBy(books => books.map(book => book.groupId)),
mergeMap(innerObs => innerObs.skip(1).map(books => books.map(book => book.groupTitle)));
然而,它并没有奏效。在这种情况下如何按 groupId 分组? (Observable<Object[]>)
最佳答案
你几乎明白了。
尝试在 groupBy()
之前使用 mergeMap()
或 concatMap()
。
举个例子:
const people = [
{ name: 'Sue', age: 25 },
{ name: 'Joe', age: 30 },
{ name: 'Frank', age: 25 },
{ name: 'Sarah', age: 35 }
];
of(people) // <- this gets Observable<Object[]>
.pipe(
mergeMap(res => res), // <- use concatMap() if you care about the order
groupBy(person => person.age, p => p.name),
mergeMap(group => zip(of(group.key), group.pipe(toArray())))
)
.subscribe(console.log);
输出:
(2) [25, Array(2)]
(2) [30, Array(1)]
(2) [35, Array(1)]
关于angular - RXJS groupBy Observable <对象[]>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50332149/