0

I am trying to get the value from nested array. Some how I was not able to get the value.

Here is my 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
                    }
                ]
            }
        ]
    }

I am trying to access Username from getData Here is my ts code

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

Here is my HTML

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

Please let me know what I am doing wrong here.

3
  • 2
    Surely you need to query via studyLevel? Commented Mar 18, 2020 at 13:55
  • 1
    do you want to display all the username? bcs i am seeing in your html you are iterating a loop for displaying a user name Commented Mar 18, 2020 at 14:08
  • @YashRami Yes correct Commented Mar 18, 2020 at 14:08

2 Answers 2

1

you can try like this. here as you can see we have an array of object the studyLevel and inside that we have another array of the object getData so we are using the two *ngFor loop one inside another

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>
Sign up to request clarification or add additional context in comments.

3 Comments

Can you please help me on the above JSON request
@Mahadevan i updated my answer let me know if it is not working
<th colspan="5"></th> <ng-container *ngFor="let row of responsearray.studyLevel"> <ng-container *ngFor="let col of row.trialDesignElements"> <th [attr.colspan]="keycount" class="trial-names">Treatment length ({{col.treatmentLength}}) </th> </ng-container> </ng-container>
0

studyLevel array has desired data:

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

UPDATE:

You can create a nested *ngFor:

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

As a suggestion, you could rename responseuserName to responseuserNames.

The work stackblitz example can be seen here.

8 Comments

after adding this also the data is not populating this is my html <ng-container *ngFor="let item of responseuserName"> <th *ngIf="item" [attr.colspan]="keycount" class="user-names">{{item.username}}</th> </ng-container>
for that you only need to assign this.responseuserName = data.studyLevel[0].getData @Mahadevan
@Mahadevan is it ok for you?:)
@StepUp I am getting error :( Cannot read property 'studyLevel' of undefined
@Mahadevan have you seen stackblitz example?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.