gpt4 book ai didi

angular - RXJS groupBy Observable <对象[]>

转载 作者:太空狗 更新时间:2023-10-29 17:13:42 25 4
gpt4 key购买 nike

我有entries$: Observable<BooksOverviewGroup[]>; :

enter image description here

我想按 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com