Angular 复杂可观测阵列排序
我有一个可观察的数组,Angular 复杂可观测阵列排序,angular,observable,Angular,Observable,我有一个可观察的数组,clientService.clients$,我想对其进行排序,但在尝试对其进行排序时似乎什么都没有发生: sortClients(method : string) : void { console.log(method) this.clientService.clients$.pipe( map( clients => { console.log(clients); sorted = [] for ( let
clientService.clients$
,我想对其进行排序,但在尝试对其进行排序时似乎什么都没有发生:
sortClients(method : string) : void {
console.log(method)
this.clientService.clients$.pipe(
map( clients => {
console.log(clients);
sorted = []
for ( let type of this.appointment_types ) {
let apps = this.clients[type];
if (method == 'name') {
sorted = _.sortBy(apps, (client) => {
if (client && client.name) {
return _.last(client.name.split(" "))
}
})
} else {
sorted = apps.sort((a, b) => {
return new Date(a['time']).valueOf() - new Date(b['time']).valueOf()
})
// console.log(sorted)
}
}
})
);
我希望避免在这个组件中存储clients
的值,而只需修改clients$
的值,这样我就可以将它作为一个可观察对象包含在我的视图中
<ion-list *ngFor="let clientGroup of clientService.clients$ | async | keyvalue; let i = index;" class="awaken-striped">
<client-index-row
*ngFor="let client of clientGroup.value"
[client]="client"
[type]="type"
(clientSelected)="itemTapped($event)"
></client-index-row>
</ion-list>
我知道有些地方不对劲,因为我无法订阅映射的结果,但我不知道如何更正。另外,我在控制台中看到了console.log(方法)
,但我没有看到console.log(客户端)
您没有从map
返回sorted
,函数map
希望您返回(变异的)数据
pipe
中调用的运算符的结果也需要应用/分配到某个对象。在这种情况下,它们将被忽略,如果类型匹配(从映射返回),您可以重新分配回客户端$
,也可以分配给新的本地字段
现在从组件而不是从服务访问字段。这假设调用了sortClients
方法,因此$clients
不是未定义的
<ion-list *ngFor="let clientGroup of clients$ | async | keyvalue; let i = index;" class="awaken-striped">
您没有从map
返回sorted
,函数map
希望您返回(变异的)数据。当我将返回sorted
添加到map
的末尾时,没有任何变化。您还需要对管道的结果进行处理。它应用于结果,然后需要将其分配回某个对象或直接使用。见下面我的答案。还要确保调用sortClients
,我之所以提到这一点,是因为您的问题中没有这样做的代码,我必须假设它是在未显示的代码中完成的。它似乎仍然没有进入map
原因console.log(客户端)
仍然不是loggedal,所以看起来文档不会返回任何@Jeremythonas的内容-它们会返回,但是如果省略大括号{}
因为您只有一行代码,所以返回是隐含的。可能检查this.clientService.clients$
是否未定义可能这就是您不输入管道
或映射
方法的原因
<ion-list *ngFor="let clientGroup of clients$ | async | keyvalue; let i = index;" class="awaken-striped">