问题
[{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.
回答1:
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.
来源:https://stackoverflow.com/questions/37717401/how-to-group-json-data-in-angularjs