DavidAlzate88 DavidAlzate88 - 1 year ago 48
AngularJS Question

filter data by type and show first item of every type

I have this array:

Controller:

$scope.data = [
{id:1, name:"a", type: "letter"},
{id:2, name:"1", type: "number"},
{id:3, name:"b", type: "letter"},
{id:4, name:"2", type: "number"},
{id:5, name:"c", type: "letter"},
{id:6, name:"3", type: "number"},
];
$scope.dataLimit = 1;


html:

<div ng-repeat="user in data | orderBy: 'type' | limitTo: dataLimit">
<label ng-bind="'name: ' + user.name"></label>
<label ng-bind="'type: ' + user.type"></label>




I tried to filter data by type and I want to show the very first data of every type, in this case would be:


  • name: a type: letter

  • name: 1 type: number


Answer Source

Use groupBy filter which is a part of angular-filter

Here is your example https://jsfiddle.net/24sk3ye5/

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download