1

I have the following data sample: sample data

for now i have two nested loop, but what i want to do is, Is it possible to loop through specific key, such as *ngFor="let key of myArray.functionalArea"

My current code is:

<span *ngFor="let jobByField of countOfJobsByFields">
  <span  *ngFor="let jobByWorkType of countOfJobsByFields?.workType">
    <li (click)="searchByWorkType(jobByWorkType?._id)"><a>{{ jobByWorkType?._id }}<span class="badge badge-default pull-right item-plus-icon-custom-style">{{ jobByWorkType.total }}</span></a></li>
 </span>

and for second key i have this:

<span *ngFor="let jobByField of countOfJobsByFields">
  <span  *ngFor="let jobByFunctionalArea of countOfJobsByFields?.functionalArea">
    <li (click)="searchByFunctionalArea(jobByFunctionalArea?._id)"><a>{{ jobByWorkType?._id }}<span class="badge badge-default pull-right item-plus-icon-custom-style">{{ jobByFunctionalArea.total }}</span></a></li>
 </span>

Note i can not use the same <span *ngFor="let jobByField of countOfJobsByFields"> for both because they are separate element in my html.

7
  • you want to iterate each functionalArea, from array? Commented Nov 13, 2017 at 5:59
  • let me know if this is helpful for you, plnkr.co/edit/KTJbPUzrfUftKhcAU29b?p=preview Commented Nov 13, 2017 at 6:00
  • @rish updated my question. Commented Nov 13, 2017 at 6:03
  • @rish plunker is the same thing i have done already. Commented Nov 13, 2017 at 6:05
  • @rish now check this, plnkr.co/edit/cxze8ymG0VyZKEmMrqQK?p=preview Commented Nov 13, 2017 at 6:09

3 Answers 3

1

Use following code

 <span *ngFor="let jobByField of countOfJobsByFields">
<div *ngIf='jobByField.workType'>
    <span  *ngFor="let jobByWorkType of jobByField.workType">
        {{jobByWorkType._id}}
    </span>
</div>

<div  *ngIf='jobByField.functionalArea'>
    <span  *ngFor="let jobByWorkType of jobByField.functionalArea">
        {{jobByWorkType._id}}
    </span>
</div>

where JSON:-

countOfJobsByFields = [
      {
        workType: [{'_id':'Part Time', 'total':10}, {'_id': 'Full Time', 'total': 20}]
      },
      {
        functionalArea: [{'_id':'IT', 'total':10}, {'_id': 'Management', 'total': 20}]
      }

    ]

Plunker

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

1 Comment

Thanks for your effort, but the same thing i have already done, with two loop.
0

Yes you can :

<span *ngFor="let jobByField of countOfJobsByFields">
    <ng-template *ngIf='jobByField.workType'>
        <span  *ngFor="let jobByWorkType of jobByField.workType">
            {{jobByWorkType._id}}
        </span>
    </ng-template>

    <ng-template *ngIf='jobByField.functionalArea'>
        <span  *ngFor="let jobByWorkType of jobByField.functionalArea">
            {{jobByWorkType._id}}
        </span>
    </ng-template>
</span>

2 Comments

countOfJobsByFields is a long list array, i do not want to use from 0, 1, etc..., because if order of array items change, it needs to change my code.
@jones, please check updated answer, I think that can help you
0

Use following code with ng-container ,it will avoid the extra div that has been used with *ngIf

<span *ngFor="let jobByField of countOfJobsByFields">
    <ng-container *ngIf='jobByField.workType'>
        <span  *ngFor="let jobByWorkType of jobByField.workType">
            {{jobByWorkType._id}}
        </span>
    </ng-container>

    <ng-container *ngIf='jobByField.functionalArea'>
        <span  *ngFor="let jobByWorkType of jobByField.functionalArea">
            {{jobByWorkType._id}}
        </span>
    </ng-container>
</span>`

Comments

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.