Angular 如何在rxJS中展平或合并阵列

Angular 如何在rxJS中展平或合并阵列,angular,rxjs,observable,angular2-observables,rxjs-lettable-operators,Angular,Rxjs,Observable,Angular2 Observables,Rxjs Lettable Operators,我有一个结构,我想把它展平成一个均匀的数组。源阵列如下所示: [ { "countryCode": "CA", "countryName": "Canada", "states": [ { "stateCode": "CAAB", "stateName": "Alberta", "countryCode": "CA", "stateAbbrev": "AB" }, .

我有一个结构,我想把它展平成一个均匀的数组。源阵列如下所示:

[
  {
    "countryCode": "CA",
    "countryName": "Canada",
    "states": [
      {
        "stateCode": "CAAB",
        "stateName": "Alberta",
        "countryCode": "CA",
        "stateAbbrev": "AB"
      },
      . . . 
      {
        "stateCode": "CAYT",
        "stateName": "Yukon Territory",
        "countryCode": "CA",
        "stateAbbrev": "YT"
      }
    ]
  },
  {
    "countryCode": "US",
    "countryName": "USA",
    "states": [
      {
        "stateCode": "USAK",
        "stateName": "Alaska",
        "countryCode": "US",
        "stateAbbrev": "AK"
      },
      . . .
      {
        "stateCode": "USWY",
        "stateName": "Wyoming",
        "countryCode": "US",
        "stateAbbrev": "WY"
      }
    ]
  }
]
[
  {
    "value": "CA",
    "label": "Canada"
  },
  {
    "value": "CACB",
    "label": "Alberta"
  },
  . . .
  {
    "value": "CAYT",
    "label": "Yukon Territory"
  },
  {
    "value": "US",
    "label": "USA"
  },
  {
    "value": "USAK",
    "label": "Alaska"
  },
  . . .
  {
    "value": "USWY",
    "label": "Wyoming"
  }
]
我想将其转换为如下所示:

[
  {
    "countryCode": "CA",
    "countryName": "Canada",
    "states": [
      {
        "stateCode": "CAAB",
        "stateName": "Alberta",
        "countryCode": "CA",
        "stateAbbrev": "AB"
      },
      . . . 
      {
        "stateCode": "CAYT",
        "stateName": "Yukon Territory",
        "countryCode": "CA",
        "stateAbbrev": "YT"
      }
    ]
  },
  {
    "countryCode": "US",
    "countryName": "USA",
    "states": [
      {
        "stateCode": "USAK",
        "stateName": "Alaska",
        "countryCode": "US",
        "stateAbbrev": "AK"
      },
      . . .
      {
        "stateCode": "USWY",
        "stateName": "Wyoming",
        "countryCode": "US",
        "stateAbbrev": "WY"
      }
    ]
  }
]
[
  {
    "value": "CA",
    "label": "Canada"
  },
  {
    "value": "CACB",
    "label": "Alberta"
  },
  . . .
  {
    "value": "CAYT",
    "label": "Yukon Territory"
  },
  {
    "value": "US",
    "label": "USA"
  },
  {
    "value": "USAK",
    "label": "Alaska"
  },
  . . .
  {
    "value": "USWY",
    "label": "Wyoming"
  }
]
到目前为止,我已经:

let countries:Observable<ICountry[]> = 
   this.http.get<ICountry[]>(`${this.buildProUrl}/states`);

return countries.map(o => o.map(c => 
  <IStateDropDownItem>{value: c.countryCode, label: c.countryName}));
let国家:可观察=
this.http.get(`this.buildProUrl}/states`);
返回国家.map(o=>o.map(c=>
{value:c.countryCode,label:c.countryName});

似乎应该有一种方法将属于每个国家的国家合并到所产生的可观测阵列中。我已经阅读了concatMap、mergeMap和switchMap文档,但我不太明白如何将它们放在一起。

我认为您只需要处理结果数组,这可以使用
Arry.reduce()函数完成。
函数:

const数据=[
{
“国家代码”:“CA”,
“国家名称”:“加拿大”,
“国家”:[
{
“州代码”:“CAAB”,
“州名”:“阿尔伯塔省”,
“国家代码”:“CA”,
“stateAbbrev”:“AB”
},
{
“州代码”:“CAYT”,
“州名”:“育空地区”,
“国家代码”:“CA”,
“stateAbbrev”:“YT”
}
]
},
{
“国家代码”:“美国”,
“国家名称”:“美国”,
“国家”:[
{
“州代码”:“USAK”,
“州名”:“阿拉斯加”,
“国家代码”:“美国”,
“stateAbbrev”:“AK”
},
{
“州代码”:“USWY”,
“州名”:“怀俄明州”,
“国家代码”:“美国”,
“stateAbbrev”:“WY”
}
]
}
];
console.log(data.reduce)((res,curr)=>{
res.push({value:curr.countryCode,label:curr.countryName});
返回res.concat(当前状态)reduce((当前状态)=>{
res.push({value:curr.stateCode,label:curr.stateName});
返回res;
}, [])); 

}, []));国家。map
是一个
可观察的.map
,而
o.map
是一个
Array.map
。请解释下行投票