Angular rxjs映射创建副本
在这个映射中,我试图更新索引上的数组。这似乎是在制造一个复制品。想法Angular rxjs映射创建副本,angular,rxjs,Angular,Rxjs,在这个映射中,我试图更新索引上的数组。这似乎是在制造一个复制品。想法 const updateAirport$ = this.updateAirportSubject$ .pipe( filter(airport => !!airport), switchMap(airport => this.airportService.updateAirport(airport)), tap(airport => {
const updateAirport$ = this.updateAirportSubject$
.pipe(
filter(airport => !!airport),
switchMap(airport => this.airportService.updateAirport(airport)),
tap(airport => {
this.snackBar.open(`${airport.name} updated`, null, snackBarConfig);
this.saveStatus = 2;
}),
map(airport => {
const index = this.airports.findIndex(item => item.id === airport.id);
this.airports = [...this.airports, this.airports[index] = airport];
return this.airports;
}),
tap(() => this.saveStatus = 0),
shareReplay()
);
然后:
this.airports$ = airports$.pipe(
merge(newAirport$, updateAirport$, uploadCSV$)
);
这是一个很难诊断的问题,因为我没有完整的解决方案,下面的分析将基于我的一些假设,如果错误,请纠正我 您的映射逻辑正在这里创建单个索引的不可变副本
this.airports = [...this.airports, this.airports[index] = airport];
然后,将其输出合并到此处
merge(newAirport$, updateAirport$, uploadCSV$)
如果this.updateAirportSubject$
中的逻辑基于newAirport$
的副本,则您正在将一个不可变副本放回自身中。。。如果是这种情况,请在合并之前从newAirport$
中删除原始索引,或在不创建不可变副本的情况下修改原始索引
修订版
仔细看,我相信这一逻辑是你的问题的原因,我不太清楚你在这方面试图实现什么
- 您正在创建this.airports的不可变副本并附加
末尾的索引副本
this.airports = [...this.airports, this.airports[index] = airport];
airport.id
const index = this.airports.findIndex(item => item.id === airport.id);
然后使用索引在airport上进行布尔匹配,以确保索引与发出的值匹配,然后附加到this.airports的末尾
this.airports[index] = airport
示例
下面的逻辑
array = [1,2,3,4,5,6,7,8,9]
index;
ngOnInit(){
this.index = this.array.indexOf(4);
this.array = [...this.array, this.array[this.index] = 4];
console.log(this.array);
}
结果就是这个输出
1,2,3,4,5,6,7,8,9,4
重复更新?同一个索引被更新了两次?什么的重复?当我运行这个时,索引被更新,我从165数组长度变为166。检查数组,有一个重复的项目更新请参阅修订我的答案