【问题标题】:get value from nested array json angular从嵌套数组json角度获取值
【发布时间】:2020-03-18 22:56:24
【问题描述】:

我正在尝试从嵌套数组中获取值。有些我无法获得价值。

这是我的 JSON

                {
        "VersionNum": "",
        "studyLevel": [{
                "countryName": "StudyLevel",
                "FS": "15-JAN-2020",
                "LS": "10-Jan-2020",
                "FI": "08-DEC-2019",
                "LI": "10-FEB-2019",
                "getData": [{
                        "countryId": 0,
                        "userId": "4",
                        "Username": "Vimal",
                        "FI": "12-JAN-2020",
                        "LI": "21-Feb-2020",
                        "experience": 5
                    },
                    {
                        "countryId": 0,
                        "userId": "5",
                        "Username": "Jack",
                        "FI": "12-JAN-2020",
                        "LI": "21-Feb-2020",
                        "experience": 3
                    },
                    {
                        "countryId": 0,
                        "userId": "6",
                        "Username": "James",
                        "FI": "12-JAN-2020",
                        "LI": "21-Feb-2020",
                        "experience": 1
                    }
                ]
            },
            {
                "countryName": "Country 1",
                "FS": "15-JAN-2020",
                "LS": "10-Jan-2020",
                "FI": "08-DEC-2019",
                "LI": "10-FEB-2019",
                "getData": [{
                        "countryId": 0,
                        "userId": "4",
                        "Username": "Paul",
                        "FI": "12-JAN-2020",
                        "LI": "21-Feb-2020",
                        "experience": 5
                    },
                    {
                        "countryId": 0,
                        "userId": "4",
                        "Username": "Smith",
                        "FI": "12-JAN-2020",
                        "LI": "21-Feb-2020",
                        "experience": 5
                    },
                    {
                        "countryId": 0,
                        "userId": "4",
                        "Username": "Trumble",
                        "FI": "12-JAN-2020",
                        "LI": "21-Feb-2020",
                        "experience": 5
                    }
                ]
            }
        ]
    }

我正在尝试从 getData 访问用户名这是我的 ts 代码

const getUserData = this.dataService.getuserList().subscribe((data:any) => {
  console.log(data);
  this.reponsearray = data;   
  this.responseuserName = data.getData[0].Username;   
});

这是我的 HTML

      <ng-container *ngFor="let item of responseuserName ">
           <th  [attr.colspan]="keycount" class="User-names">{{item.userName}}</th>
      </ng-container>

请让我知道我在这里做错了什么。

【问题讨论】:

  • 确定要通过studyLevel查询?
  • 要显示所有用户名吗? bcs 我在您的 html 中看到您正在迭代循环以显示用户名
  • @YashRami 是的,正确

标签: angular angular8


【解决方案1】:

你可以这样试试。如您所见,我们有一个对象数组studyLevel,在其中我们有另一个对象数组getData,所以我们使用两个*ngFor循环一个内一个

TS

const getUserData = this.dataService.getuserList().subscribe((data:any) => {
  console.log(data);
  this.responseuserName = data.studyLevel;   
});

HTML

 <ng-container *ngFor="let item of responseuserName ">
     <ng-container *ngFor="let data of intem. getData >
           <th  [attr.colspan]="keycount" class="User-names">
{{data.userName}}</th>
     </ng-container>
 </ng-container>

【讨论】:

  • 你能帮我解决上面的 JSON 请求吗
  • @Mahadevan 我更新了我的答案让我知道它是否不起作用
  • 治疗长度 ({{col.treatmentLength}}) 跨度>
【解决方案2】:

studyLevel 数组有想要的数据:

const getUserData = this.dataService.getuserList().subscribe((data:any) => {
  console.log(data);
  this.reponsearray = data;   
  this.responseuserName = data.studyLevel[0].getData[0].Username;   
});

更新:

您可以创建一个嵌套的*ngFor

<div *ngFor="let row of response.studyLevel">
  <div *ngFor="let col of row.getData">
    {{col.Username}} {{col.FI}} {{col.LI}}
  </div>
</div>

作为建议,您可以将 responseuserName 重命名为 responseuserNames

The work stackblitz example can be seen here.

【讨论】:

  • 添加后数据也没有填充这是我的html {{item.username}}
  • 为此你只需要分配this.responseuserName = data.studyLevel[0].getData@Mahadevan
  • @Mahadevan 你可以吗?:)
  • @StepUp 我收到错误:(无法读取未定义的属性“studyLevel”
  • @Mahadevan 你看过 stackblitz 的例子吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-10
  • 2019-03-04
  • 1970-01-01
相关资源
最近更新 更多