I have an array of items, each with a category that is sometimes shared. What I want to do is group the items in my template by their category. Currently I'm achieving this by simply looping the array for each category, but that's inefficient. I want to loop the array once only, and output to the different sections respectively. I can't seem to search a clear way to do this. Here's a rough outline of what I'm hoping to do:
<ng-container *ngFor="let item of items">
<!--
The checkbox below needs to be output to one of the proceeding
4 sections based on its category, I assume using a switch.
-->
<mat-checkbox
class="item-toggle"
[checked]="item.enabled"
(change)="toggleItem(item)"
>{{ item.name }}</mat-checkbox
>
</ng-container>
<h3>Category 1</h3>
<section class="item-choices" #category1></section>
<h3>Category 2</h3>
<section class="item-choices" #category2></section>
<h3>Category 3</h3>
<section class="item-choices" #category3></section>
<h3>Category 4</h3>
<section class="item-choices" #category4></section>
Is there a way to do this with the default directives available?
Edit: Updated the example. I wanted to highlight that I do not wish to duplicate the category sections. I wanted to direct the output to the 4 separate locations. I can achieve this now by either running 4 loops, once for each category. Or, in my ts, I can filter the array into subarrays for each category.
I was hoping there would be a way to do this with the template or directives alone.