将JSON接口转换为数组

时间:2018-12-07 09:56:16

标签: javascript angularjs json typescript

我有一个接口类和JSON文件,我想将其转换为列表并对其进行处理。例如,将JSON中每个对象的Racename放入list \ array。 可能吗? 这就是界面:

interface IRunners{
Racename: string;
Category:number; 
Gender:string; 
Work:string; 
FullName:string; 
Rank:number;
Point:number;
Numparticipant:number;
rankparticipant:number;
precentagePart:string; 
NumRaces:number; 
RaceTime:string; 
rankCat:number; 
PointCat:number; 
RaceDate:string;
}

这是JSON文件(Runners.json):

[
  {"Racename":"A1","Category":34,"Gender":"זכר","Work":"AMDOCS","FullName":"Simon Work ","Rank":1,"Ponit":1,"Numparticipant":0,"rankparticipant":0,"precentagePart":"0","NumRaces":1,"RaceTime":"2018-10-18T00:34:20","rankCat":1,"PointCat":1,"RaceDate":"2018-10-05"}
] 

我正在按以下方式订阅:

  this.runnerService.getRunners().subscribe(
        runners=>{
          this.runners = runners;
          this.filteredCompetitions = this.runners;
          this.filteredRunners = this.runners;
        }

我想将runners JSON转换为数组,以便进行一些更改并从中获取一些数据。 我是Typescript和Angular的新手,所以我可能会犯一些错误。

1 个答案:

答案 0 :(得分:2)

您可以使用Array#map()

// Assumes runners.json is a file in the same directory as this file
const jsonArray = require("./runners.json")

// // returns ["A1"]
const listOfRacenames = jsonArray.map(d => d.Racename)