如何 return 来自 JSON 对象数组的特定字段 angular http 服务作为响应
How to return specific fields from a JSON object array in angular http service as response
我的界面是这样的:
export interface User {
id: number;
name: string;
}
我从 api 收到的回复是:
[
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz"
},
{
"id": 2,
"name": "Ervin Howell",
"username": "Antonette",
"email": "Shanna@melissa.tv"
}
]
我希望仅提取 ID 和名称字段以及 return 作为响应
[
{
"id": 1,
"name": "Leanne Graham"
},
{
"id": 2,
"name": "Ervin Howell"
}
]
getdata(): Observable<User[]>{
return this.http.get<User[]>('https://jsonplaceholder.typicode.com/users').pipe(
map((data: User[])=> {
//how to extract id and name here
})
)
}
我只需要 return 来自
整个 api 响应。我如何使用地图实现这一目标
或服务中的任何其他技术,请指导我
getdata(): Observable<User[]>{
return this.http.get<User[]>('https://jsonplaceholder.typicode.com/users').pipe(
map((data: User[])=> {
// just map the data
return data.map(u => ({id: u.id, name: u.name}))
})
)
}
我的界面是这样的:
export interface User {
id: number;
name: string;
}
我从 api 收到的回复是:
[
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere@april.biz"
},
{
"id": 2,
"name": "Ervin Howell",
"username": "Antonette",
"email": "Shanna@melissa.tv"
}
]
我希望仅提取 ID 和名称字段以及 return 作为响应
[
{
"id": 1,
"name": "Leanne Graham"
},
{
"id": 2,
"name": "Ervin Howell"
}
]
getdata(): Observable<User[]>{
return this.http.get<User[]>('https://jsonplaceholder.typicode.com/users').pipe(
map((data: User[])=> {
//how to extract id and name here
})
)
}
我只需要 return 来自 整个 api 响应。我如何使用地图实现这一目标 或服务中的任何其他技术,请指导我
getdata(): Observable<User[]>{
return this.http.get<User[]>('https://jsonplaceholder.typicode.com/users').pipe(
map((data: User[])=> {
// just map the data
return data.map(u => ({id: u.id, name: u.name}))
})
)
}