如何在angularjs中对json数据进行分组

时间:2016-06-09 05:20:14

标签: javascript angularjs json jsfiddle angularjs-filter

  [{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中的数据进行分组或过滤。任何帮助,将不胜感激。

1 个答案:

答案 0 :(得分:4)

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}
    ];

详细信息,have a look at this answer