映射列表中每个项目的单个JSON属性

huangapple go评论63阅读模式
英文:

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&lt;Team[]&gt;{
   return this.httpClient.get&lt;Team[]&gt;(this.teamsServiceURL).pipe(
      map((res:any)=&gt;{
        return res.data.map((x:any)=&gt;({
          ...x,
          fullName:x.full_name  //&lt;--see this line
        }))
      })
   )
}

And change the component call to service to be as follows :

this.teamService.getTeams().subscribe({
      next: (response: Team[]) =&gt; {
        this.teams = response;
      },
      error: (e) =&gt; console.error(e),
      complete: () =&gt; console.info(&#39;complete&#39;) 
    })

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)=&gt;({
          ...x,
          fullName:x.data.full_name  //&lt;--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&lt;Team&gt;{
   return this.http.get&lt;TeamResponse&gt;(this.teamsServiceURL).pipe(
      map((teams) =&gt; teams.map(
          team =&gt; {
              return {
                 ...team,
                 fullName: team.full_name 
              }
          }
      ))
   );
}
英文:
getTeams(): Observable&lt;Team&gt;{
   return this.http.get&lt;TeamResponse&gt;(this.teamsServiceURL).pipe(
      map((teams) =&gt; teams.map(
          team =&gt; {
              return {
                 ...team,
                 fullName: team.full_name 
              }
          }
      ))
   );
}

huangapple
  • 本文由 发表于 2023年2月8日 15:19:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75382476.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定