javascript - 根据 Object 值对 Observable 数组进行排序
问题描述
我正在从 firestore 返回一个 Observable 数组,并希望按我在数据库对象中创建的索引对它们进行排序。
它们存储在数据库中,如下所示:
然后它们根据键 ID(按字母顺序)在 UI 中显示,但是,我希望它们根据索引在 UI 中排序。但是,我确实需要将 id 作为初始键。
我已经尝试映射值并使用 sort() 来按下面 ode 中显示的索引排序,但仍然以随机顺序返回。
这就是我使用 map 尝试按索引的键值排序的方式。
this.bedrooms$ = this.property$.pipe(
flatMap((property: any) => this._property.getAllBedrooms(property.bedrooms, this.propertyId))
);
this.sortedBedroom$ = this.bedrooms$.pipe(
map((room: any) => {
return room.sort((a: any, b: any) =>
b.index - a.index
);
}
)
);
解决方案
您的问题可能与此行有关,b.test - a.index
而不是b.index - a.index
.
AngularFirestore
我会为此建议另一种解决方案,使用from获取从 firestore 中排序的数据@angular/fire
:
this.afs.collection<Bedroom>('bedrooms', ref => ref.orderBy('index'))
推荐阅读
- android - 带有数据库和网络请求的分页库
- couchdb - 我可以使用 Hyperledger Couchdb 混合排序吗?
- android - 如何处理免安装应用中的所有应用链接
- elasticsearch - 全域弹性搜索的配置
- android - Firebase 云消息传递 (FCM) onMessageReceived 永远不会在服务中被调用
- android - 在弹出菜单中更改文本颜色
- mysql - 如何在 SQL 中的行中查找重复项?
- ruby-on-rails - pg_search 用于自动完成或子字符串搜索
- ios - 更改导航栏中按钮的大小
- polymer - Polymer- 带有子分页的应用程序路由