本文介绍了如何在angularjs中对json数据进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
[{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
我在网上查了一下,但找不到对这种结构进行分组的方法.有没有办法以上述格式对angularjs中的数据进行分组或过滤.任何帮助,将不胜感激.
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.
推荐答案
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}
];
有关详细信息,看看这个答案.
这篇关于如何在angularjs中对json数据进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!