[{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"}];
我想以这样的方式对我的数据进行分组:我得到以下结构:
name:A
team alpha: 10
team beta: 20
team gamma:40
name:B
team alpha:15
team beta:25
name:C
team alpha:30
我在线检查但找不到将这种结构分组的方法。有没有办法可以用上面提到的格式对angularjs中的数据进行分组或过滤。任何帮助,将不胜感激。
答案 0 :(得分:4)
<强> 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}
];