如何在返回外部可观察对象之前修改可观察对象发出的每个对象的内部元素
How to modify inner element of each Object emitted by observable before returning the outer observable
我对 Observables 和 rxjs 的整个概念不熟悉,所以这可能非常明显。但是请帮忙,因为我很想学习
我有一个功能,当用户点击显示博客下的评论时 post
showComments(){
this.firestoreservice.getComments(this.job.id).subscribe( data =>{
this.comments = data //type [] of comment objects with (message,posterID,timeposted...)
})
this._showcomments = true;
}
此函数调用一个服务函数,该函数 returns 可观察到我对该作业的评论列表。
在订阅那个可观察的是初始化一个变量来保存我所有的评论。
然而,在我这样做之前,有没有办法用匹配的用户名替换每个评论的 posterID 属性?请参阅下面的预期结果:
showComments(){
this.firestoreservice.getComments(this.job.id).subscribe( data =>{
//for each comment held in data, replace comment.posterID with getUserName(posterID)
//getUserName(posterID) will also return an observable which emmits one string
//then append comment to comments list (or add back in to data then destructively assign
//comments=data )
this.comments = data //type [] of comment objects with (message,posterID,timeposted...)
})
this._showcomments = true;
}
看起来有点像这样....
this.firestoreservice.getComments(this.job.id).pipe(
switchMap(comments => { // switchMap to subscribe to inner
const getUsers$ = comments.map(comment => // array map comments into userName fetch
this.firestoreservice.getUserName(comment.posterID).pipe(
// once you have the username, rx map back into the comment with the username assigned
map(userName => ({...comment, posterID: userName})),
first() // use to complete after one emission to make forkJoin work
)
)
return forkJoin(...getUsers$); // forkJoin them all together for parallel execution
});
).subscribe(data => {
this.comments = data // now just set the data
})
我对 Observables 和 rxjs 的整个概念不熟悉,所以这可能非常明显。但是请帮忙,因为我很想学习
我有一个功能,当用户点击显示博客下的评论时 post
showComments(){
this.firestoreservice.getComments(this.job.id).subscribe( data =>{
this.comments = data //type [] of comment objects with (message,posterID,timeposted...)
})
this._showcomments = true;
}
此函数调用一个服务函数,该函数 returns 可观察到我对该作业的评论列表。 在订阅那个可观察的是初始化一个变量来保存我所有的评论。 然而,在我这样做之前,有没有办法用匹配的用户名替换每个评论的 posterID 属性?请参阅下面的预期结果:
showComments(){
this.firestoreservice.getComments(this.job.id).subscribe( data =>{
//for each comment held in data, replace comment.posterID with getUserName(posterID)
//getUserName(posterID) will also return an observable which emmits one string
//then append comment to comments list (or add back in to data then destructively assign
//comments=data )
this.comments = data //type [] of comment objects with (message,posterID,timeposted...)
})
this._showcomments = true;
}
看起来有点像这样....
this.firestoreservice.getComments(this.job.id).pipe(
switchMap(comments => { // switchMap to subscribe to inner
const getUsers$ = comments.map(comment => // array map comments into userName fetch
this.firestoreservice.getUserName(comment.posterID).pipe(
// once you have the username, rx map back into the comment with the username assigned
map(userName => ({...comment, posterID: userName})),
first() // use to complete after one emission to make forkJoin work
)
)
return forkJoin(...getUsers$); // forkJoin them all together for parallel execution
});
).subscribe(data => {
this.comments = data // now just set the data
})