如何在Angular中访问JSON数组中的数组

时间:2018-12-07 06:57:24

标签: angular

我想在我的component.ts中访问json数组中的数组。我尝试了普通的for循环,但我无法访问它。我也尝试了foreach但它给出了错误。我想访问我的组件中的配置文件类型。 ts文件。我在for(让经验中的个人资料)出现错误。 请给我解决办法

json文件

"Experiences": [ {
            "ExperienceId": 1,
            "Title": "GIS Analyst",
            "ExperienceType": "Professional",
            "FromDate": "2011-05-04",
            "TodaysDate": "2011-05-06",
            "Renumerations": ["20","50"],
            "Organization" :{
                "OraganizationId": 1,
                "OrganizationName": "Here",
                "Location": "Airoli"
            },
            "Profiles" :[ {
                    "Title": "Hi",
                    "ProfileType": "Operations", 
                    "FromDate": "2011-05-04",
                    "ToDate": "2011-05-04",
                    "CustomerName": "Here",
                    "Location": "Airoli",
                    "Roles": [{
                        "RoleName": "Data analysis",
                        "Responsibilities": [{
                            "ResponsibilityId": 1,
                            "Responsibility": "Find requirements",
                            "Description": "It is the practice of finding the requirements"
                        }]
                    }
                ]
                }]
            }]

component.ts

 this.personListService. getExperienceDetail()
     .subscribe(
      resp =>{ 
       this.experiences = resp.Experiences,
 var i,j;
          for(i=0;i<this.experiences.length;i++)
          {
            this.experiencetypes[i]=this.experiences[i].ExperienceType
            this.fromdateexps[i]=new Date(this.experiences[i].FromDate)
            this.todateexps[i]=new Date(this.experiences[i].TodaysDate)

            //this.rangeValues[i]=Number(this.experiences[i].Renumeration)

          }
       for(let experience in this.experiences)
       {
        for(let profile in experience)
        {
          this.profiletypes[i]=this.experiences[i].Profile[j].ProfileType
          console.log(this.profiletypes) 
        }
       }

1 个答案:

答案 0 :(得分:0)

您有几个错误。

var i,j number; 

for(i=0;i<this.experiences.length;i++){

    this.experiencetypes[i]=this.experiences[i].ExperienceType
    this.fromdateexps[i]=new Date(this.experiences[i].FromDate)
    this.todateexps[i]=new Date(this.experiences[i].TodaysDate)
    //this.rangeValues[i]=Number(this.experiences[i].Renumeration)

    for(j=0;j<this.experiences[i].Profile.length;j++){

        this.profiletypes[i][j]=this.experiences[i].Profile[j].ProfileType
        console.log(this.profiletypes) 

    }
}