也许是一个简单的问题,但我找不到这方面的例子。
这是我的HttpClient调用
getItems(dataSourceUrl: string, bindKey: string, bindValue: string): Observable<SelectItem[]> {
return this.httpClient.get<Array<any>>(dataSourceUrl);
}
我想基于bindKey
和bindValue
将列表结果映射到SelectItem []。我怎么做?
我试过这样的事
return this.httpClient.get<Array<any>>(dataSourceUrl).pipe(map(x=> { return { label: data.bindKey, value: data.bindValue } }));
接口
export interface SelectItem {
label: string;
value: any;
}
两个不同的api响应的示例
1.
{key:'Istanbul', value: 'Test' }
{key:'London', value: 'Test' }
bindKey
将是key
bindValue
将是value
2.
{name:'Istanbul', id: 'Test' }
{name:'Istanbul', id: 'Test' }
bindKey
将是id
bindValue
将是name
尝试
getItems(dataSourceUrl: string, bindKey: string, bindValue: string): Observable<SelectItem[]> {
return this.httpClient.get<SelectItem[]>(dataSourceUrl)
.pipe(map(x=> this.transformValue(bindKey,bindValue,x)));
}
transformValue(bindKey,bindValue,response):SelectItem[]{
const newResponse = [];
response.forEach(data => {
newResponse.push({
label: data[bindKey],
value: data[bindValue]
})
})
return newResponse;
}