RXjs groupBy - 组 AngularFire Firestore 集合 [英] RXjs groupBy - Group AngularFire Firestore collection
问题描述
我正在尝试按字段对 AngularFire
Firestore 集合 (Observable[]) 进行分组,例如 userId
.
我有一个项目集合,每个项目都有不同的 userId 值,我需要将它们分组到一个多维数组中.所以,转换..
<预><代码>[{用户ID:1,颜色:'蓝色'},{userId:2, color:'green'},{用户 ID:1,颜色:'橙色'}]进入
<预><代码>[[{用户ID:1,颜色:'蓝色'},{用户 ID:1,颜色:'橙色'}],[{用户 ID:2,颜色:'绿色'}]]我是 RxJs 的新手,我知道在制作 Observable.of(itemsArray)
时 groupBy
操作符是如何工作的,但我看不到如何让它与从 AgularFire
Observable
一起工作let items:Observable[] = this.afs.collection- ('items').snapshotChanges().groupBy( item => item.userId );//item实际上是数组
它看起来像 AngularFire
返回一个 Observable
,它作为一个整体发出 Array,而不是每个元素.
如果我map
它,那么我是不是只有数组中的每个项目,而不能使用groupBy
?
关于如何返回上述分组数组的 Observable
数组的任何指针?
您可以简单地将数组的 observable 转换为数组值的 observable:
myObservable.flatMap(arr => Rx.Observable.from(arr));
I'm trying to group a AngularFire
Firestore Collection (Observable[]) by a field, say userId
.
I have a collection of items, each with varying userId values, and I need to group them into a multidimensional array. So, convert..
[
{userID:1, color:'blue' },
{userId:2, color:'green'},
{userId:1, color:'orange'}
]
into
[
[
{userID:1, color:'blue' },
{userId:1, color:'orange'}
],
[
{userId:2, color:'green'}
]
]
I'm totally new to RxJs, I get how the groupBy
operator works when making an Observable.of(itemsArray)
, but I can't see how to get this to work with the Observable
returned from AgularFire
let items:Observable[] = this.afs.collection<Item>('items')
.snapshotChanges()
.groupBy( item => item.userId ); //item is actually the array
It looks like AngularFire
returns an Observable
that emits the Array as a whole, not each element.
If I map
it, dont I then just have each item in the array, and cant use the groupBy
?
Any pointers on how to return a Observable
array of grouped arrays as per above?
You can simply turn your observable of array into an observable of the values of the array with:
myObservable.flatMap(arr => Rx.Observable.from(arr));
这篇关于RXjs groupBy - 组 AngularFire Firestore 集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!