Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Angular 2中执行groupBy操作?_Javascript_Angular_Typescript_Group By - Fatal编程技术网

Javascript 如何在Angular 2中执行groupBy操作?

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;

角度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;
                  });
此查询返回此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
);