Angular在另一个数组中获取JSON数组

时间:2019-08-09 21:08:38

标签: arrays json angular

我有以下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 )


    })
  }

2 个答案:

答案 0 :(得分:3)

f['daypart']是一个数组。因此,您需要按其索引引用它。

this.dforecast = f['daypart'][0]['narrative'];

此外,由于我们正在处理可观察对象而不是基于承诺的响应,因此无需将forcast()声明为async方法。

forcast() {
 // rest of your cose 
}

答案 1 :(得分:2)

您应该尝试:

this.dforecast = f["daypart"][0]["dayOrNight"]

因为它是一个数组。

相关问题