Here's the code : plnkr.co
HTML:
<!DOCTYPE html>
<html ng-app="example">
<head>
<script data-require="[email protected]" data-semver="1.11.3" src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script data-require="[email protected]" data-semver="1.2.28" src="https://code.angularjs.org/1.2.28/angular.js"></script>
</head>
<body ng-controller="mainCtrl">
<div ng-repeat="(key, val) in obj | groupPlayers">
{{key}} = {{ val }}
</div>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
script.js
(function() {
angular
.module('example', [])
.controller('mainCtrl', function($scope) {
$scope.obj = [
{ lastname: 'Jackson', firstname: 'George', score: 6},
{ lastname: 'Jackson', firstname: 'George', score: 10},
{ lastname: 'Smith', firstname: 'Michael', score: 8},
{ lastname: 'Smith', firstname: 'Amanda', score: 2},
{ lastname: 'Smith', firstname: 'Michael', score: 7},
{ lastname: 'Doe', firstname: 'John', score: 4},
];
})
.filter('groupPlayers', function() {
return function(list, search) {
var group = {};
var key;
angular.forEach(list, function(item) {
key = item.lastname + ' ' + item.firstname;
if(!group[key])
group[key] = [];
group[key].push(item.score);
});
return group;
}
})
;
})();
I want to group items of an object to display score player by player. I create a angular filter to do this work. The display is fine but I have an error in the console and I don't find the problem after an hour of analyzing !
Error : 10 $digest() iterations reached. Aborting!
Thanks