首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从嵌套数组json angular获取值

从嵌套数组json angular获取值
EN

Stack Overflow用户
提问于 2020-03-18 21:54:42
回答 2查看 58关注 0票数 0

我正在尝试从嵌套数组中获取值。一些我无法获得值的原因。

这是我的JSON

代码语言:javascript
复制
                {
        "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代码

代码语言:javascript
复制
const getUserData = this.dataService.getuserList().subscribe((data:any) => {
  console.log(data);
  this.reponsearray = data;   
  this.responseuserName = data.getData[0].Username;   
});

这是我的HTML

代码语言:javascript
复制
      <ng-container *ngFor="let item of responseuserName ">
           <th  [attr.colspan]="keycount" class="User-names">{{item.userName}}</th>
      </ng-container>

请让我知道我做错了什么。

EN

回答 2

Stack Overflow用户

发布于 2020-03-18 22:12:00

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

TS

代码语言:javascript
复制
const getUserData = this.dataService.getuserList().subscribe((data:any) => {
  console.log(data);
  this.responseuserName = data.studyLevel;   
});

HTML

代码语言:javascript
复制
 <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>
票数 1
EN

Stack Overflow用户

发布于 2020-03-18 22:00:03

studyLevel阵列具有所需的数据:

代码语言:javascript
复制
const getUserData = this.dataService.getuserList().subscribe((data:any) => {
  console.log(data);
  this.reponsearray = data;   
  this.responseuserName = data.studyLevel[0].getData[0].Username;   
});

更新:

您可以创建嵌套的*ngFor

代码语言:javascript
复制
<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.

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60741046

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档