2

I know I may sound a bit confusing so I request you to please let me elaborate

Note: A working POC (piece of code) has been attached to this post.

I have an array of an object having 3 properties name

  1. name
  2. team
  3. team_rank
 $scope.players = [
      {name: 'Gene', team: 'alpha', team_rank: 1},
      {name: 'George', team: 'beta', team_rank: 2},
      {name: 'Steve', team: 'gamma', team_rank: 3},
      {name: 'Paula', team: 'beta', team_rank: 2},
      {name: 'Scruath', team: 'gamma', team_rank: 3}
    ];

I have got the result shown below in the pic by running the POC.

enter image description here

Problem

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

<!DOCTYPE html>
<html ng-app="app">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/angular-filter/0.4.7/angular-filter.js"></script>
  
<meta name="description" content="[groupBy example]"/>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  <div ng-controller="MainController"> 
    
<ul ng-repeat="(key, value) in players | groupBy: 'team'">
  Group name: {{ key }}
  <li ng-repeat="player in value">
    player: {{ player.name }}
  </li>
</ul>
  </div>

<footer>
  <script>
angular.module('app',['angular.filter'])
  .controller('MainController', function($scope) { 
    $scope.players = [
      {name: 'Gene', team: 'alpha', team_rank: 1},
      {name: 'George', team: 'beta', team_rank: 2},
      {name: 'Steve', team: 'gamma', team_rank: 3},
      {name: 'Paula', team: 'beta', team_rank: 2},
      {name: 'Scruath', team: 'gamma', team_rank: 3}
    ];
 });
  </script>
</footer>
</body>
</html>

2
  • i think POC stands for Proof of Concept Commented Jul 9, 2016 at 10:30
  • @eyurdakul Depends on the situation :) you can call it a homonym Commented Jul 9, 2016 at 10:32

2 Answers 2

2

If you're sure every player has the team_rank property, you can do:

Group name: {{ key }} Rank: {{value[0].team_rank}}

<!DOCTYPE html>
<html ng-app="app">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/angular-filter/0.4.7/angular-filter.js"></script>
  
<meta name="description" content="[groupBy example]"/>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  <div ng-controller="MainController"> 
    
<ul ng-repeat="(key, value) in players | groupBy: 'team'">
  Group name: {{ key }} Rank: {{value[0].team_rank}}
  <li ng-repeat="player in value">
    player: {{ player.name }}
  </li>
</ul>
  </div>

<footer>
  <script>
angular.module('app',['angular.filter'])
  .controller('MainController', function($scope) { 
    $scope.players = [
      {name: 'Gene', team: 'alpha', team_rank: 1},
      {name: 'George', team: 'beta', team_rank: 2},
      {name: 'Steve', team: 'gamma', team_rank: 3},
      {name: 'Paula', team: 'beta', team_rank: 2},
      {name: 'Scruath', team: 'gamma', team_rank: 3}
    ];
 });
  </script>
</footer>
</body>
</html>

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

Comments

1

Method 1:

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

Method 2:

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

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.