I'm relatively new to Angular 2 and I need to sort/group an array of objects based on the first letter of their name. eg.:
{
"type": "fruit",
"name": "apple",
},
{
"type": "fruit",
"name": "banana",
},
{
"type": "vegetable",
"name": "broccoli",
},
{
"type": "fruit",
"name": "orange",
},
I would like it to show it like:
<h3>A</h3>
<ul>
<li>apple</li>
</ul>
<h3>B</h3>
<ul>
<li>banana</li>
<li>broccoli</li>
</ul>
<h3>O</h3>
<ul>
<li>orange</li>
</ul>
I found this post: How to group data in Angular 2 ?.
I tried it like:
<div *ngFor="let item of food | groupBy:'name.charAt(0)'">
<h3>{{name.charAt(0) | uppercase}}</h3>
<li>...</li>
</div>
I know i probably need a pipe, but i cant get it to work.
Hope someone can help