Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.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
在angular7中,如何从两个不同的依赖http调用获得组合响应。第一个http调用返回对象及其相关数据的数组_Angular_Typescript_Rxjs_Angular7 - Fatal编程技术网

在angular7中,如何从两个不同的依赖http调用获得组合响应。第一个http调用返回对象及其相关数据的数组

在angular7中,如何从两个不同的依赖http调用获得组合响应。第一个http调用返回对象及其相关数据的数组,angular,typescript,rxjs,angular7,Angular,Typescript,Rxjs,Angular7,我的第一个api返回以下数据 { "resp": [ { "id": "932e", "name": "jon" }, { "id": ... "name": .., } ] } 我必须循环遍历这个数组,并使用上面的id对API进行另一个调用,该id返回如下数据 { "

我的第一个api返回以下数据

{
"resp": [
  {
    "id": "932e",
    "name": "jon"        
  },
  {
    "id": ...
    "name": ..,
  }
]       
}

我必须循环遍历这个数组,并使用上面的id对API进行另一个调用,该id返回如下数据

{
"posts": [
  {
    "id": "1111",
    "title": "post on facebook"        
  },
  {
    "id": "2222",
    "title": "post on covid"        
  }

]      
public getUsersPost(userId) {

     return this.datasetService.getPosts(userId).pipe(
      map(ds => {
        return ds.posts
      }));
  }
决赛 数据应采用这种格式

{
  "resp": [
    {
      "id": "932e",
      "name": "jon",
      "posts": [
        {
          "id": "1111",
          "name": "post on facebook"
        },
        {
          "id": "2222",
          "name": "post on covid"
        }
      ]
    }   
  ]
}
调用第二个api的方法的签名如下

{
"posts": [
  {
    "id": "1111",
    "title": "post on facebook"        
  },
  {
    "id": "2222",
    "title": "post on covid"        
  }

]      
public getUsersPost(userId) {

     return this.datasetService.getPosts(userId).pipe(
      map(ds => {
        return ds.posts
      }));
  }
不确定如何使用map、mergreMap或concatMap for>


提前感谢

您选择的映射运算符取决于您想要的加载策略
mergeMap
并行发送所有请求(您可以限制同时请求的最大数量),而
concatMap
将串行执行这些请求。我还建议将第一个数组拆分为其组成部分

我怀疑你想要的是这样的东西:

const populatedUsers$=this.getUsers.pipe(
mergeMap(userArray=>from(userArray)),//现在分别发送每个用户)
mergeMap(user=>this.getUsersPost(user.id).pipe(
映射(ds=>({…用户,posts:ds.posts}))
),null,5),//最多5个并发请求
toArray()//将所有内容转换回单个数组
);

映射运算符的选择取决于所需的加载策略
mergeMap
并行发送所有请求(您可以限制同时请求的最大数量),而
concatMap
将串行执行这些请求。我还建议将第一个数组拆分为其组成部分

我怀疑你想要的是这样的东西:

const populatedUsers$=this.getUsers.pipe(
mergeMap(userArray=>from(userArray)),//现在分别发送每个用户)
mergeMap(user=>this.getUsersPost(user.id).pipe(
映射(ds=>({…用户,posts:ds.posts}))
),null,5),//最多5个并发请求
toArray()//将所有内容转换回单个数组
);

谢谢威尔。你的回答很准确,不客气。你介意接受答案吗?这样你的问题看起来像是已经回答的,其他人可以更容易地找到它。谢谢威尔。你的回答很准确,不客气。您是否介意接受答案,以便您的问题看起来像已回答的,而其他人可以更容易地找到它?