1

I have an array of an object having 3 properties name

1.name 2.team 3.age

http://jsfiddle.net/HpTDj/46/

I want group with both age and team.i done group with team but i have confusion how i need to group with both team and age in my current code.

I need to show the age of the team with the team name. Please suggest what should I do?

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>
1
  • @ Maxim Shoustin : yes but i want solution how to modify my code Commented Aug 31, 2017 at 10:38

1 Answer 1

0

you can do it like this

 <ul ng-repeat="(key, value) in MyList | groupBy: '[team,name]'">
 <b>Group name: {{key}}</b>
 <br>
 <li ng-repeat="player in value">
  <b>player:</b> {{ player.name }} 
  <br>
  <b>team:</b> {{player.team}} 
 </li>
</ul>

For more details you can see this

Sign up to request clarification or add additional context in comments.

6 Comments

Groupby is not working some reason in my application you have idea to archive in my code ??
you have idea it would be really helpful
you also want to group by with name?
im not understand pls guide me where i need to change my code to archive this
what you want to do?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.