Angular2/Typescript“;“地图”;两个观测值对应一个界面

Angular2/Typescript“;“地图”;两个观测值对应一个界面,angular,rxjs,typescript2.0,angular2-observables,Angular,Rxjs,Typescript2.0,Angular2 Observables,我在将两个数据源合并到一个接口时遇到了一个小问题 有一个接口 export interface IProvince { code: number; name: string; svgData: string; part: number; color: string; } 现在我有两个不同的数据源,一个为我提供字段的数据:[code,name,svgData,part],[color]来自另一个可观察对象 getAllProvince(): Observable<IPr

我在将两个数据源合并到一个接口时遇到了一个小问题

有一个接口

export interface IProvince {
  code: number;
  name: string;
  svgData: string;
  part: number;
  color: string;
}
现在我有两个不同的数据源,一个为我提供字段的数据:[code,name,svgData,part],[color]来自另一个可观察对象

getAllProvince(): Observable<IProvince[]>
  {
    return <Observable<IProvince[]>>this.http
      .get('url')
      .map((response: Response) => <IProvince[]>response.
      .catch(this.handleError)
      .finally(() => {
        this.loader.complete();
        console.log("Data: ", this.provinceData);
      });

  }
  getLegendData(): Observable<ILegend[]> {
    return Observable.of(this.legendData);
  }

  getStyleColorData(part: number) {
    let color: ILegend;
    return this.getLegendData().subscribe((items: ILegend[]) => color = items.find(p => p.part == part));
  }
getAllProvince():可观察
{
返回此文件。http
.get('url')
.map((响应:响应)=>响应。
.接住(这个.把手错误)
.最后(()=>{
this.loader.complete();
console.log(“数据:”,this.provinceData);
});
}
getLegendData():可观察{
可观测的返回(此legendData);
}
getStyleColorData(部分:编号){
让颜色:ILegend;
返回这个.getLegendData().subscribe((items:ILegend[])=>color=items.find(p=>p.part==part));
}
问题是,目前我缺少数据[color],我不知道如何“动态”添加另一个函数的值

这里我有一个函数:getStyleColorData(part:number),在我从[getAllProvince()]获得[part]之后,它应该给我“color”。 问题:有一种简单的方法可以将这两者结合起来,或者我走错方向了


答案和解决方案的发送:)

如果您有两个可观察对象(例如来自http请求,您可以将它们与或组合

试着这样做:

getProvinces() {
  return this.http
    .get('url')
    .map(response => response.json() as IProvince[])
    .withLatestFrom(this.getLegendData(), (provinces, legendData) => ({provinces, legendData})
    .map(combined => combined.provinces.map(
            province => province.color = combined.legendData.find(legend => legend.part == province.part).color));
}

getLegendData() {
  return this.http
    .get('url2')
    .map(response => response.json() as ILegend[]);
}

这是什么。legendData?一个简单的数组?如何从ILegend中获取颜色字符串?现在是数组,但应该是来自服务器的另一个数据。例如:{part:1,name:“Quantyl 1”,color:“#B4572C”},我尝试了这个,但是当做类似thah:L combineMapdata(){Observable.combinelatetest(this.getAllProvince(),this.getLegendData)的事情时()).subscribe((data:any[]):void=>{console.log(data);//casteds-data[0]//glass-data[1]});}ii获得“控制台/服务器垃圾邮件,每秒请求一次,我相信它的sth具有可观察性我添加了一个示例,在尝试类似的操作时,如果问题仍然存在,您可以尝试吗?现在是:(属性'color'在类型'iprovance'上不存在。)我相信当我“第一次映射我的颜色停止时,在上下文中存在,