how to group json data in angularjs

给你一囗甜甜゛ 提交于 2019-12-23 19:20:13

问题


  [{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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!