-1
  1. List item:
jsonObject = [
    {
      place: 'place1',
      fatherDetails: [
        {name: 'place1_father1', adharId: 134567},
        {name: 'place1_father2', adharId: 124567},
      ],
      motherDetails: [
        {name: 'place1_mother1', adharId: 123456},
        {name: 'place1_mother2', adharId: 123457},
        {name: 'place1_mother3', adharId: 123467},
      ],
    },
    {
      place: 'place2',
      fatherDetails: [
        {name: 'place2_father1', adharId: 123567},
        {name: 'place2_father2', adharId: 12567},
      ],
      motherDetails: [
        {name: 'place2_mother1', adharId: 1234567},
        {name: 'place2_mother2', adharId: 1234567},
        {name: 'place2_mother3', adharId: 1234567},
      ],
    }
  ];

How do I loop all the fatherDetails objects in HTML with ngFor? Basically, I require something like this:

<mat-option *ngFor = "let father of jsonObject">
   {{father.name}}
</mat-option>

But, this doesn't work. Because, it has to loop twice.

6
  • That's not Json Commented Oct 14, 2019 at 5:12
  • Why is it not JSON object? Commented Oct 14, 2019 at 5:48
  • @MaruthiEranki before you ask for something, please mention what you have tried, do you have done any research, and do you know how to use ngFor, if you your basic is not clear refer https://angular.io/guide/structural-directives. Commented Oct 14, 2019 at 5:58
  • I tried and also mentioned what I've tried. Commented Oct 14, 2019 at 6:20
  • Possible duplicate of Angular 2 using nested ngFor Commented Oct 14, 2019 at 7:07

4 Answers 4

1

This can be easily achieved using Angular's ng-container.

    <ng-container *ngFor="let object of jsonObject>
        <mat-option *ngFor = "let father of object.fatherDetails">
            {{father.name}}
        </mat-option>
    </ng-container

ng-container won't clutter your DOM with unnecessary divs and allows you to access that nested array.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. This helped me and I was looking for something like ng-container.
1

Note You need to iterate the first loop and the second loop will be iterated inside the first loop's array of object.

jsonObject = [{
    place: 'place1',
    fatherDetails: [{
        name: 'place1_father1',
        adharId: 134567
      },
      {
        name: 'place1_father2',
        adharId: 124567
      },
    ],
    motherDetails: [{
        name: 'place1_mother1',
        adharId: 123456
      },
      {
        name: 'place1_mother2',
        adharId: 123457
      },
      {
        name: 'place1_mother3',
        adharId: 123467
      },
    ],
  },
  {
    place: 'place2',
    fatherDetails: [{
        name: 'place2_father1',
        adharId: 123567
      },
      {
        name: 'place2_father2',
        adharId: 12567
      },
    ],
    motherDetails: [{
        name: 'place2_mother1',
        adharId: 1234567
      },
      {
        name: 'place2_mother2',
        adharId: 1234567
      },
      {
        name: 'place2_mother3',
        adharId: 1234567
      },
    ],
  }
];
<tr *ngFor="let object of jsonObject;let i = index;">
  <td>{{i}}</td>
  <td * ngFor="let o of object.motherDetails">{{o}}</td>
</tr>

Comments

1

Just extract fatherDetails from all items, flat array and iterate over it

In componeng class:

 this.allfathersDetails = this.jsonObject.map( item => item.fatherDetails).flat();

In a template:

 <div *ngFor="let details of allfathersDetails">
 ...

2 Comments

Thank you, Is there any option without creating a variable?
You can try to use <div *ngFor="let details of jsonObject.map( item => item.fatherDetails).flat()"> but may be bad for perfomance
0

You can try this code.

<div *ngFor = "let object of jsonObject">
<div *ngFor ="let father of object.fatherDetails>
<span>{{father.name}}</span>
</div>
</div>

5 Comments

Yeah. It works with <div>. But, I'm working with <mat-select> and <mat-option>.
You can write first ngFor in mat-select and second ngFor in mat-option.If it works than please upvote.
Dude, it's displaying multiple select boxes which means it is looping the select and not looping values in select box.
You can store fatherdetails array in other array in ts file.
Yeah. That's how I'm working now presently and trying to avoid using extra variable. Thank you.

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.