angular - 将数组表达式转换为对象角度
问题描述
我有这个从 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>
我怎样才能实现它?
解决方案
this.substitutes = res.substitutions;
this.players = res.players;
应该
this.substitutes = res[0].substitutions;
this.players = res[0].player;
推荐阅读
- c++ - 友元定义函数的命名空间是什么?
- json - Discord.py bot 给出错误:TypeError:列表索引必须是整数或切片,而不是 str
- ios - TableView Row 在点击时改变颜色
- django - Django - NoReverseMatch - 使用关键字参数'{'api':'9933124422'}'的'contextual'反向
- ruby-on-rails - 如何修复输入到表单中的 Ruby on Rails 特殊字符。(ò, ā)
- react-native - 我找不到合适的 api 来在我的应用程序中实现聊天?
- haskell-stack - 程序编译时 Idris REPL 找不到导入
- java - Java Selenium CI 构建运行速度比本地快得多?
- swift - “StorageMetaData”类型的值没有成员 SWIFT
- python - 将 SpaCy 的 EntityRecognizer 应用于 Pandas 数据框中的列