3

Having problems with groupBy filter of angular-filter module in a nested array. The 'location' is undefined and 'color' isn't outputting anything. Took this example and modified it from someone else's code.

Thanks in advance!

<!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>
<script>
angular.module('app',['angular.filter']).controller('MainController', function($scope) { 
$scope.players = [
  {name: 'Gene', team: 'alpha'[
            {location: 'texas',
            color: 'blue'}]},
  {name: 'George', team: 'beta'[
            {location: 'texas',
            color: 'red'}]},
  {name: 'Steve', team: 'gamma'[
            {location: 'kansas',
            color: 'purple'}]},
  {name: 'Paula', team: 'beta'[
            {location: 'kansas',
            color: 'green'}]},
  {name: 'Scruath', team: 'gamma'[
            {location: 'kansas',
            color: 'orange'}]}
];
});
</script>

<title>JS Bin</title>
</head>
<body>
<div ng-controller="MainController"> 

<ul ng-repeat="(key, value) in players | groupBy: 'team.location'">
Group name: {{ key }}
<li ng-repeat="player in value">
player: {{ player.name }}  <br/>
favorite color: {{ player.color }} 
</li>
</ul>
</div>
</body>
</html>

1 Answer 1

4

Your data structure doesn't correspond to your code logic. This works fine:

<!DOCTYPE html>
<html ng-app="app">
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-filter/0.4.7/angular-filter.js"></script>
        <script>
            angular.module('app',['angular.filter']).controller('MainController', function($scope) { 
            $scope.players = [
              {name: 'Gene',
                team: {location: 'texas',
                      color: 'blue'}
              },
              {name: 'George', team: {location: 'texas',
                        color: 'red'}},
              {name: 'Steve', team: 
                        {location: 'kansas',
                        color: 'purple'}},
              {name: 'Paula', team: 
                        {location: 'kansas',
                        color: 'green'}},
              {name: 'Scruath', team:
                        {location: 'kansas',
                        color: 'orange'}}
            ];
            });
        </script>

        <title>JS Bin</title>
    </head>
    <body>
        <div ng-controller="MainController"> 
            <ul ng-repeat="(key, value) in players | groupBy: 'team.location'">
                Group name: {{ key }}
                <li ng-repeat="player in value">
                player: {{ player.name }}  <br/>
                favorite color: {{ player.color }} 
                </li>
            </ul>
        </div>
    </body>
</html>

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.