我写了RXJS运算符来获取一系列位置。
这是我的代码:
return O$ = this.db.list(`UserPlaces/${this.authData.auth.auth.currentUser.uid}`, {
query: {
orderByChild: 'deleted',
equalTo: false
}
})
.map((locations: any) => {
return locations.map(location => {
return location.$key;
});
}).switchMap(ids => {
return ids.map(id => {
return this.db.object(`Devices/${id}`)
});
})
.flatMap((x: any) => {
return x;
})
.map((x:any)=>{
if(!x.deleted){
return x;
}
})
.scan((acc:any, item) => [...acc, item],[])
.do(console.log)
给定的数组按顺序填充:
[{}] --> [{},{}] --> [{},{},{}] --> [{},{},{},{},...]
我需要的是直接获取完整数组:
[{},{},{},{},...]
我尝试使用forkJoin但不起作用:
return Observable.forJoin(O$) //but doesn't work.
有任何帮助来解决这个问题吗?
感谢。
答案 0 :(得分:0)
您使用subscribe
方法的全部功能订阅它,并等到调用complete
回调或抛出错误:
const o$ = (...) // your final observable
let array = null;
o$.subscribe({
next: value => array = value,
complete: () => {
// the operation behind the observable completed. No more values
// will be yielded. 'array' contains the final values
},
error: (error) => {
// there was an error. No more values will be yielded
}
});
答案 1 :(得分:0)
将.scan更改为reduce()
.map((x:any)=>{
if(!x.deleted){
return x;
}
})
.reduce((acc:any, item) => {
acc.push(item)
return acc
},[])