angular - 在 Map 之前按嵌套数组中的值排序
问题描述
使用 NG6 Rxjs 通过 http 返回一些数据。然后我将返回的数据分成两个 Observable。效果很好,但我需要先按嵌套数组中的值对数据进行排序。我可以在视图中触发事件后对数组进行排序,但在页面加载时不起作用。
此代码按外部数组中的值排序:
return this.http
.get(this.apiUrl + 'PatchGroups/ForApp/' + appId)
.pipe(
map((data: Array<any>) => {
return {
ungrouped: data.filter(d => d.id < 0),
grouped: data.filter(d => d.id > 0).sort((a, b) => a.name < b.name ? -1 : 1)
};
}),
catchError(this.handleError)
);
尝试按嵌套值排序,但效果不佳:
return this.http
.get(this.apiUrl + 'PatchGroups/ForApp/' + appId)
.pipe(
map((data: Array<any>) => {
return {
ungrouped: data.sort((a, b) => a.server.name < b.server.name ? -1 : 1).filter(d => d.id < 0),
grouped: data.filter(d => d.id > 0).sort((a, b) => a.name < b.name ? -1 : 1)
};
}),
catchError(this.handleError)
);
对每个 Observable 进行排序似乎很脏。我不能在传递给每个 Observable 之前对数据进行排序吗?
从我的组件:
this.appsApiService.getPatchGroups(appReconId)
.subscribe(pg => {
this.ungrouped_patchGroups = pg.ungrouped;
this.grouped_patchGroups = pg.grouped;
}
);
解决方案
推荐阅读
- ios - UIActivityViewController 在 Twitter (v7.33) 上共享文本 + URL + 图像在 iOS 12 上不起作用
- android - 当我合并分支时,Android Gradle 插件无法识别 SourceSet 'instrumentTest'
- c++ - 使用递归 C++ 打印出模式
- bash - 在 10 个终端中运行循环命令
- shell - cd 到目录不起作用,但 ls 起作用 - 为什么会发生这种情况?
- javascript - 如何在 RibbonBarButton 的 onclick 上执行一个 js 函数
- augmented-reality - Galaxy Tab S4 与 ARCore 不兼容吗?
- c++ - OpenCV傅里叶变换复杂输出问题
- android - 谷歌关于短信、通话记录权限的新政策后如何获取信息?
- java - 使用 Spring Security 将现有帐户与 OAuth2 提供程序链接/取消链接