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;
}, []));
}, []));如果我理解正确,这与RxJS无关;您不想合并流,只想更改流中的对象。因此,RxJS文档对您毫无帮助,您需要编写一个普通的TS/JS函数,该函数接受您的输入结构并返回您的输出结构,然后将其映射到您的流中。这与RxJS无关。您只需要将一组国家/地区转换为一组IStateDropDownItems。因此,您所需要的只是已经在使用的.map()运算符,您必须向它传递一个函数,该函数将一个国家数组转换为一个IStateDropDownItems数组。您可能会感到困惑,因为您使用的是两个不同的映射-国家。map
是一个可观察的.map
,而o.map
是一个Array.map
。请解释下行投票