3
  [{name: 'A', team: 'team alpha', value:"10"},
  {name: 'A', team: 'team beta' value:"20"},
    {name: 'A', team: 'team gamma' value:"40"},
  {name: 'B', team: 'team alpha' value:"15"},
  {name: 'B', team: 'team beta' value:"25"},
  {name: 'C ', team: 'team alpha' value:"30"}];

I want to group my dat in such a way that i get the following structure:

    name:A
    team alpha: 10
    team beta: 20
    team gamma:40

   name:B
    team alpha:15
    team beta:25

   name:C
   team alpha:30

I check online but could not find a way to group this kind of structure. Is there a way I can group or filter the data in angularjs in the above mentioned format. Any help would be appreciated.

2
  • Do you need to get the values according to team names? Did't you try loop and conditions? Commented Jun 9, 2016 at 5:40
  • @DinithMinura That would complicate things a lot and does not seem to be a feasible solution. Commented Jun 9, 2016 at 5:55

1 Answer 1

4

Working Demo

HTML

<ul ng-repeat="(key, value) in players | groupBy: 'name'">
  <li>name: {{ key }}
     <ul>
        <li ng-repeat="player in value">
          {{ player.team }} : {{player.value}} 
        </li>
     </ul>
  </li>
</ul>

JavaScript:

$scope.players = [
      {name: 'A', team: 'team alpha', value:10},
      {name: 'A', team: 'team beta',value:20},
      {name: 'A', team: 'team gamma',value:40},
      {name: 'B', team: 'team alpha',value:15},
      {name: 'B', team: 'team beta',value:25},
      {name: 'C', team: 'team alpha',value:30}
    ];

For detail, have a look at this answer.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.