[英]Map data array for a specific object array type
Maybe a easy question, but I couldn't find a example for this.也许是一个简单的问题,但我找不到一个例子。
This is my HttpClient call这是我的 HttpClient 调用
getItems(dataSourceUrl: string, bindKey: string, bindValue: string): Observable<SelectItem[]> {
return this.httpClient.get<Array<any>>(dataSourceUrl);
}
I want to map the list result to SelectItem[] based on bindKey
and bindValue
.我想根据
bindKey
和bindValue
将列表结果映射到 SelectItem[] 。 How do I do that?我怎么做?
I tried something like this我试过这样的事情
return this.httpClient.get<Array<any>>(dataSourceUrl).pipe(map(x=> { return { label: data.bindKey, value: data.bindValue } }));
Interface界面
export interface SelectItem {
label: string;
value: any;
}
Example of two different api responses两个不同的 api 响应示例
1. 1.
{key:'Istanbul', value: 'Test' }
{key:'London', value: 'Test' }
bindKey
will be key
bindValue
will be value
bindKey
将是key
bindValue
将是value
2. 2.
{name:'Istanbul', id: 'Test' }
{name:'Istanbul', id: 'Test' }
bindKey
will be id
bindValue
will be name
bindKey
将是id
bindValue
将是name
Try尝试
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.