英文:
Map single json property for each item in a list
问题
以下是您要翻译的内容:
"fullName" 属性在 "Team" 接口中无法正常工作,因为我想将其映射到 JSON 中的 "full_name"。
如何最佳地将 "full_name" 映射到 "fullName",而不是在接口中直接使用 "full_name",因为这种命名约定不被推荐使用?
希望这有所帮助。如果您需要更多信息,请告诉我。
英文:
I am new to Angular and I am using Angular 14
1- JSON response :
{
"data": [
{
"id": 1,
"abbreviation": "ATL",
"city": "Atlanta",
"conference": "East",
"division": "Southeast",
"full_name": "Atlanta Hawks",
"name": "Hawks"
},
{
"id": 2,
"abbreviation": "BOS",
"city": "Boston",
"conference": "East",
"division": "Atlantic",
"full_name": "Boston Celtics",
"name": "Celtics"
}
}
- Model Interface :
export interface TeamResponse{
data: Team[];
}
- Team Interface :
export interface Team{
id: Number;
abbreviation: string;
city: string;
conference: string;
division: string;
name: string;
fullName: string; // I wan to map this to full_name json property
}
- Service:
getTeams(): Observable<TeamResponse>{
return this.http.get<TeamResponse>(this.teamsServiceURL);
}
- Component Typescript File:
export class TeamSelectComponent implements OnInit {
selectedTeam! : Team;
teams : Team[] = [];
constructor(private teamService:TeamService) { }
ngOnInit() {
this.teamService.getTeams().subscribe({
next: (response: TeamResponse) => {
this.teams = response.data;
},
error: (e) => console.error(e),
complete: () => console.info('complete')
})
}
}
Everything works file except for the fullName property in Team interface since I want to map it to full_name in json.
What is the best way to map full_name to fullName instead of easily using full_name in the interface ?
since this naming convention is not recommended.
答案1
得分: 1
以下是您要翻译的内容:
"If you have not access to your API you can change a bit your service to return an array of Teams
For this, use "map" in your service to return an array of Team
getTeams(): Observable<Team[]>{
return this.httpClient.get<Team[]>(this.teamsServiceURL).pipe(
map((res:any)=>{
return res.data.map((x:any)=>({
...x,
fullName:x.full_name //<--see this line
}))
})
)
}
And change the component call to service to be as follows :
this.teamService.getTeams().subscribe({
next: (response: Team[]) => {
this.teams = response;
},
error: (e) => console.error(e),
complete: () => console.info('complete')
})
Equal, when you search an unique Team you also use map, I imagine some like
getTeam(id:number){
return this.httpClient.get(...).pipe(
map((res:any)=>({
...x,
fullName:x.data.full_name //<--see this line
}))
)
}
NOTE: If you have access to your API, sure you can make that return fullName and not full_name, e.g. in .NET you can customize property names"
请注意,其中包含一些代码部分,已经被保留,未进行翻译。
英文:
If you have not access to your API you can change a bit your service to return an array of Teams
For this, use "map" in your service to return an array of Team
getTeams(): Observable<Team[]>{
return this.httpClient.get<Team[]>(this.teamsServiceURL).pipe(
map((res:any)=>{
return res.data.map((x:any)=>({
...x,
fullName:x.full_name //<--see this line
}))
})
)
}
And change the component call to service to be as follows :
this.teamService.getTeams().subscribe({
next: (response: Team[]) => {
this.teams = response;
},
error: (e) => console.error(e),
complete: () => console.info('complete')
})
Equal, when you search an unique Team you also use map, I imagine some like
getTeam(id:number){
return this.httpClient.get(...).pipe(
map((res:any)=>({
...x,
fullName:x.data.full_name //<--see this line
}))
)
}
NOTE: If you have access to your API, sure you can make that return fullName and not full_name, e.g. in .NET you can customize property names
答案2
得分: 0
我不是TypeScript专家。但我猜它期望JSON和接口的fullName键应该相同。
我的建议是避免使用下划线,而是尝试使用驼峰命名法。
英文:
I'm not an expert in typescript. But, I guess its expecting the both keys should be same in json and interface for fullName.
my suggestion, avoid using underscore instead try using camelCase.
答案3
得分: 0
getTeams(): Observable<Team>{
return this.http.get<TeamResponse>(this.teamsServiceURL).pipe(
map((teams) => teams.map(
team => {
return {
...team,
fullName: team.full_name
}
}
))
);
}
英文:
getTeams(): Observable<Team>{
return this.http.get<TeamResponse>(this.teamsServiceURL).pipe(
map((teams) => teams.map(
team => {
return {
...team,
fullName: team.full_name
}
}
))
);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论