首页 > 解决方案 > 将数组表达式转换为对象角度

问题描述

我有这个从 http 请求返回的 json

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

我怎样才能实现它?

标签: angularionic-framework

解决方案


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

应该

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

工作示例


推荐阅读