I have a structure I would like to flatten into one homogeneous array. The source array looks like this:
[
{
"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"
}
]
}
]
What I would like to transform this into should look like this:
[
{
"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"
}
]
So far I have:
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}));
It seems like there ought to be a way to merge the states belonging to each country into the resulting observable array. I've read the concatMap, mergeMap and switchMap documentation but I can't quite figure out how to put it all together.