簡體   English   中英

如何顯示 API 中的 object 數組以使其顯示

[英]how to display an array of object from an API for it to display

大家好,我在我的項目中使用 api 調用 Open AQ 平台。
HTTP/1.1 200 正常

 "results": [
  {
    "location": "Punjabi Bagh",
    "city": "Delhi",
    "country": "IN",
    "measurements": [
      {
        "parameter": "so2",
        "value": "7.8",
        "lastUpdated": "2015-07-24T11:30:00.000Z",
        "unit": "µg/m3",
        "sourceName": "Punjabi Bagh",
        "averagingPeriod": {
          "unit": "hours",
          "value": 0.25
        }
       },
       {
         "parameter": "co",
         "value": 1.3,
         "lastUpdated": "2015-08-18T23:30:00.000Z",
         "unit": "mg/m3",
         "sourceName": "CPCB",
         "averagingPeriod": {
           "unit": "hours",
           "value": 0.25
         }
       },
       {
         "parameter": "pm25",
         "value": 79,
         "lastUpdated": "2015-10-02T21:45:00.000Z",
         "unit": "µg/m3",
         "sourceName": "CPCB",
         "averagingPeriod": {
           "unit": "hours",
           "value": 0.25
         }
       }
     ]
       ...
   }
]

在我的應用程序中,它將呈現以下內容:

"location": "Punjabi Bagh",
    "city": "Delhi",
    "country": "IN",

但不會從 api 渲染 sourceName 或 averagingPeriod,它只會渲染未定義我應該如何解決這個問題。

看起來“sourceName”和“averagingPeriod”嵌套在“measurements”中,這是一個數組。

嘗試:

measurements[0].sourceName
measurements[0].averagingPeriod

measurements[1].sourceName
measurements[1].averagingPeriod

根據您的需要,您可以使用 map 獲得它們:

measurements.map(item => item.sourceName)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM