arrays - Angular在另一个数组中获取json数组
问题描述
我有以下 JSON 结构:
{
"daypart": [
{
"dayOrNight": [
null,
"N",
"D",
"N",
"D",
"N",
"D",
"N",
"D",
"N",
"D",
"N"
]
}
]
}
我想要做的是到达里面的键数组narrative
。但是每当我尝试获取这些密钥时,我都会得到预测undefined
或预测[object Object]
代码
async forcast(){
this.http.get("xxxxxxxxxxxxxxx")
.subscribe(data => {
let f = data
this.dforecast = f["daypart"]["narrative"]
console.log("forecast "+ this.dforecast )
})
}
解决方案
f['daypart']
是一个数组。因此,您需要通过它的索引来引用它。
this.dforecast = f['daypart'][0]['narrative'];
此外,不需要声明forcast()
为async
方法,因为我们处理的是可观察对象,而不是基于 Promise 的响应。
forcast() {
// rest of your cose
}
推荐阅读
- ios - 使用 XIB 在 UITextView 上设置内容插入
- javascript - 如何在单击交叉选项时从应用的过滤器中删除元素?- Reactjs/JavaScript
- javascript - Vue JS 路由器组件 - 在路由之间导航后无法重用使用 webpack 导入的 JS 文件
- c# - 414(请求 URI 太长)
- javascript - 在词法范围或 this 绑定之间使用对象方法?
- c++ - SCIP 如何在我使用 SCIP 时使用 c++ 在我的源代码中添加目标函数
- xamarin.forms - 使用 Google 登录以获取应用的 UWP 和 Xamarin.Forms (android) 版本
- c# - 在已打开的表单中填充文本框
- powershell - 查找文件和输出
- laravel - 从内存而不是磁盘加载视图数据