将数组表达式转换为Object angular

时间:2018-06-14 05:34:41

标签: angular ionic-framework

我有这个json从http请求返回

[
  {
        "player": {
            "1100": {
                "name": "Andy",
                "position": "Keeper"
            },
            "1927": {
                "name": "Cole",
                "position": "benchwarmer"
            },
            "2399": {
                "name": "Derrick",
                "position": "Striker"
            },
            "2900": {
                "name": "Cole",
                "position": "normal supporter"
            }
        },
        "substitutions": [
            {
                "playerin": 1100,
                "playerout": 1927,
                "time": 58
            },
            {
                "playerin": 2399,
                "playerout": 2900,
                "time": 58
            }
        ]
    }

]

这是我的ts

Object = Object;

this.http.get('http://example.com/example.php?matchid='+this.matchId)
            .map(res => res.json())
            .subscribe(res => { 
              this.substitutes = res[0].substitutions;
              this.players = res[0].players;


 });
循环替换时,我希望它显示玩家名称也来自,我需要将playerin playerout数据更改为对象,但未能这样做。这是我的示例失败代码,它返回未定义的玩家

//failed 1
<div *ngFor="let subH of substitutes">
    {{players[subH.playerin].name}}
    </div> 
//failed 2
<div *ngFor="let subH of Object.keys(substitutes)"> 
    {{players[subH.playerin].name}}
    </div> 

我怎样才能实现它?

1 个答案:

答案 0 :(得分:0)

this.substitutes = res.substitutions;
this.players = res.players;

应该是

this.substitutes = res[0].substitutions;
this.players = res[0].player;

Working Example