Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 转换api结果以从其他api请求收集其他数据_Angular_Rxjs - Fatal编程技术网

Angular 转换api结果以从其他api请求收集其他数据

Angular 转换api结果以从其他api请求收集其他数据,angular,rxjs,Angular,Rxjs,我有一个api调用,它返回一个结果列表作为可观察结果。最后,我想在一个列表中显示这些数据,但该列表应该包含每个记录的其他数据,而这些数据不是第一个请求附带的 我认为这是一个非常简单的问题描述:给出一个数组的可观测值,我想通过调用web服务来转换数组中的每个项,然后返回一个修改后的数组的可观测值 getActivePosts = (): Observable<Post[]> => { return this.get('/Post/Active') .pipe(

我有一个api调用,它返回一个结果列表作为可观察结果。最后,我想在一个列表中显示这些数据,但该列表应该包含每个记录的其他数据,而这些数据不是第一个请求附带的

我认为这是一个非常简单的问题描述:给出一个数组的可观测值,我想通过调用web服务来转换数组中的每个项,然后返回一个修改后的数组的可观测值

getActivePosts = (): Observable<Post[]> => {
  return this.get('/Post/Active')
    .pipe(
      map(posts => posts.map(u => ({
        title: u.title,
        author: u.author,
        rating: 0      // <- This is the value I have to look up elsewhere
       })))
    );
}
甚至在我还没弄明白调用下一个api来获得当前的评级可能更棘手的部分之前,我已经被卡住了。这个例子永远不会通过toArray,因为由from创建的内部流永远不会完成。当此代码运行并订阅此函数的结果时,外部可观察?没有发出任何东西。我可以确认初始api调用已经完成,并且devtools显示响应是一个数组,正如预期的那样


如何对数组中的每个项执行操作,但仍然返回一个数组?如果有这样的事情,我很乐意用rxjs的方式来解决这个问题。

您的用例现在已经相当普遍了。您可以使用以下代码-

getActivePosts = (): Observable<Post[]> => {
  return this.get('/Post/Active')
    .pipe(
      map(posts => posts.map(u => ({
        title: u.title,
        author: u.author,
        rating: 0      // <- This is the value I have to look up elsewhere
       }))),
      mergeMap(post => this.getRating(post[id]) //check the id
               .pipe(
                  tap(rating => post.rating = rating;
               )
       ),
    toArray()
    );
}
getActivePosts = (): Observable<Post[]> => {
  return this.get('/Post/Active')
    .pipe(
      map(posts => posts.map(u => ({
        title: u.title,
        author: u.author,
        rating: 0      // <- This is the value I have to look up elsewhere
       }))),
      mergeMap(post => this.getRating(post[id]) //check the id
               .pipe(
                  tap(rating => post.rating = rating;
               )
       ),
    toArray()
    );
}