angular - 从嵌套数组json角度获取值
问题描述
我正在尝试从嵌套数组中获取值。有些我无法获得价值。
这是我的 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
并且在里面我们有另一个对象数组,所以我们在另一个循环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>
推荐阅读
- android - 如何使用几个按钮,它们之间有依赖关系?安卓工作室
- google-cloud-platform - 将目录上传的文件类型设置到 Google 存储
- angular - 如何使用 Angular 6 更新图像
- c# - 使用 Linq 从 xml 文件读取到 objectlist
- ios - 如何使用 xib 文件正确创建自定义单元,注册并加载它?
- javascript - Gulp autoprefixer 已安装但未添加前缀
- angular - Angular 测试组件显示在测试窗口中
- java - 自动装配多个 SimpleJobLauncher
- java - 由于大量文件被复制,Java 应用程序崩溃
- momentjs - momentjs 错误地计算日期差