In my array of objects, each item has three properties:
1. name 2. team 3. age
I have successfully grouped the items by team, but I am unsure how to group them by both team and age in my current code.
I want to display the age along with the team name. Can someone please advise on how to achieve this?
JS:
$scope.MyList = [
{name: 'Gene', team: 'alpha', age: 19},
{name: 'George', team: 'beta', age: 19},
{name: 'Steve', team: 'gamma', age: 23},
{name: 'Paula', team: 'beta', age: 23},
{name: 'Scruath', team: 'gamma', age: 23},
{name: 'Scruath 1', team: 'gamma', age: 22},
{name: 'Scruath 2', team: 'gamma', age: 22}
];
$scope.getGroups = function () {
var groupArray = [];
angular.forEach($scope.MyList, function (item, idx) {
if (groupArray.indexOf(item.team) == -1){ groupArray.push(item.team)
}
});
return groupArray.sort();
}
HTML:
<div ng-repeat='group in getGroups()'>
<h2>{{group}}</h2>
<ul>
<li ng-repeat="item in MyList | groupby:group">{{item.name}}</li>
</ul>
</div>