Javascript 如何在Angular 2中执行groupBy操作?
角度2:2.0.0-alpha.31/Typescript 1.5 我从Javascript 如何在Angular 2中执行groupBy操作?,javascript,angular,typescript,group-by,Javascript,Angular,Typescript,Group By,角度2:2.0.0-alpha.31/Typescript 1.5 我从http.get查询请求数据 this.http.get('/data/players.json') .toRx() .map((res) => res.json()) .subscribe((data) => { this.players = data;
http.get
查询请求数据
this.http.get('/data/players.json')
.toRx()
.map((res) => res.json())
.subscribe((data) => {
this.players = data;
});
此查询返回此Json对象(This.players
)
然后,我想显示按团队分组的球员
teamA: player1,player2,player3
teamB: player4,player5
如何在Angular2上(直接进入@View?)查看groupBy
team
更新
我在Angular 1上看到,这可以在视图中使用ngFor上的| groupBy:'propertyName'
实现。Angular 2上有类似的东西吗?你能用过滤器吗
var test = [
{"team":"teamA","name":"player1","age":"1"},
{"team":"teamA","name":"player2","age":"1"},
{"team":"teamA","name":"player3","age":"1"},
{"team":"teamB","name":"player4","age":"1"},
{"team":"teamB","name":"player5","age":"1"}
];
function FilterByTeam(obj){
console.log(obj);
return obj.team === "teamA";
}
var teamA = test.filter(FilterByTeam);
console.log(teamA)
不确定Angular2的确切版本,但您可以致电。我认为您也不需要在较新的版本中调用
.toRx()
在订阅之前,请添加以下内容:
.map((res) => res.json())
.groupBy(
x => x.team,
x => x
);
不幸的是,我希望在视图中动态地执行此操作。我在angular1上看到,这可以通过:
player in players | groupBy:“team”
完成,我正在搜索类似的内容。谢谢
.map((res) => res.json())
.groupBy(
x => x.team,
x => x
);