Angular filter

GroupBy - Group Data in Angular Filter

First do a loop using a filter that will return only unique teams, and then a nested loop that returns all players per current team:

HTML Code looks like
<div ng-app="app">
    <div ng-controller="myCtrl">
        <li ng-repeat="(team, players) in teamPlayers | groupBy:'team'">
                <li ng-repeat="player in players">

JS Code looks like –
var app = angular.module('app', []);
app.controller('myCtrl', function($scope) {
    //Players List
    $scope.teamPlayers = [
        { name: 'Anil', team: 'A' },
        {name: 'Sushil', team: 'B'},
        {name: 'Tinku', team: 'B'},
        {name: 'Aradhya', team: 'G'},
        {name: 'Reena', team: 'G'}];

//groupBy with Players
app.filter('groupBy', function($parse) {
    return _.memoize(function(items, field) {
        var getter = $parse(field);
        return _.groupBy(items, function(item) {
            return getter(item);

Live Result

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog -
My Books - Book 1 and Book 2

GroupBy - Group Data in Angular Filter GroupBy - Group Data in Angular Filter Reviewed by Anil Singh on 9:09 PM Rating: (5) Powered by Blogger.