首页 > 解决方案 > 如何检索只有 00:00:00 时间的数据?

问题描述

我想用 React 建立一个天气预报网站。我使用 openweather API。对于 5 天预报功能,openweath 每 3 小时更新一次数据。如果我想每天显示天气信息(绝对直到 5 天后),如果我想在 JavaScript 中只抓取 00:00:00 时间的数据,如何过滤数据?我不想一直显示它。

示例我想要什么(2021-06-07 00:00:00、2021-06-08 00:00:00 等...)

以下是 API 的响应。

{
   "cod":"200",
   "message":0,
   "cnt":10,
   "list":[
      {
         "dt":1623067200,
         "main":{
            "temp":303.17,
            "feels_like":309.24,
            "temp_min":298.78,
            "temp_max":303.17,
            "pressure":1007,
            "sea_level":1007,
            "grnd_level":1006,
            "humidity":74,
            "temp_kf":4.39
         },
         "weather":[
            {
               "id":500,
               "main":"Rain",
               "description":"hujan rintik-rintik",
               "icon":"10n"
            }
         ],
         "clouds":{
            "all":40
         },
         "wind":{
            "speed":1.08,
            "deg":47,
            "gust":1.51
         },
         "visibility":10000,
         "pop":0.7,
         "rain":{
            "3h":1.1
         },
         "sys":{
            "pod":"n"
         },
         "dt_txt":"2021-06-07 12:00:00"
      },
      {
         "dt":1623078000,
         "main":{
            "temp":301.25,
            "feels_like":305.5,
            "temp_min":297.4,
            "temp_max":301.25,
            "pressure":1009,
            "sea_level":1009,
            "grnd_level":1009,
            "humidity":80,
            "temp_kf":3.85
         },
         "weather":[
            {
               "id":500,
               "main":"Rain",
               "description":"hujan rintik-rintik",
               "icon":"10n"
            }
         ],
         "clouds":{
            "all":60
         },
         "wind":{
            "speed":1.33,
            "deg":230,
            "gust":1.48
         },
         "visibility":10000,
         "pop":0.79,
         "rain":{
            "3h":1.47
         },
         "sys":{
            "pod":"n"
         },
         "dt_txt":"2021-06-07 15:00:00"
      },
      {
         "dt":1623088800,
         "main":{
            "temp":298.69,
            "feels_like":299.6,
            "temp_min":296.45,
            "temp_max":298.69,
            "pressure":1010,
            "sea_level":1010,
            "grnd_level":1008,
            "humidity":88,
            "temp_kf":2.24
         },
         "weather":[
            {
               "id":500,
               "main":"Rain",
               "description":"hujan rintik-rintik",
               "icon":"10n"
            }
         ],
         "clouds":{
            "all":80
         },
         "wind":{
            "speed":1.91,
            "deg":254,
            "gust":3.05
         },
         "visibility":10000,
         "pop":0.68,
         "rain":{
            "3h":0.12
         },
         "sys":{
            "pod":"n"
         },
         "dt_txt":"2021-06-07 18:00:00"
      },
      {
         "dt":1623099600,
         "main":{
            "temp":295.71,
            "feels_like":296.45,
            "temp_min":295.71,
            "temp_max":295.71,
            "pressure":1009,
            "sea_level":1009,
            "grnd_level":1006,
            "humidity":93,
            "temp_kf":0
         },
         "weather":[
            {
               "id":804,
               "main":"Clouds",
               "description":"awan mendung",
               "icon":"04n"
            }
         ],
         "clouds":{
            "all":100
         },
         "wind":{
            "speed":1.4,
            "deg":223,
            "gust":1.83
         },
         "visibility":10000,
         "pop":0.09,
         "sys":{
            "pod":"n"
         },
         "dt_txt":"2021-06-07 21:00:00"
      },
      {
         "dt":1623110400,
         "main":{
            "temp":295.86,
            "feels_like":296.48,
            "temp_min":295.86,
            "temp_max":295.86,
            "pressure":1010,
            "sea_level":1010,
            "grnd_level":1007,
            "humidity":88,
            "temp_kf":0
         },
         "weather":[
            {
               "id":804,
               "main":"Clouds",
               "description":"awan mendung",
               "icon":"04d"
            }
         ],
         "clouds":{
            "all":100
         },
         "wind":{
            "speed":1.24,
            "deg":201,
            "gust":1.66
         },
         "visibility":10000,
         "pop":0.06,
         "sys":{
            "pod":"d"
         },
         "dt_txt":"2021-06-08 00:00:00"
      },
      {
         "dt":1623121200,
         "main":{
            "temp":301.75,
            "feels_like":304.09,
            "temp_min":301.75,
            "temp_max":301.75,
            "pressure":1011,
            "sea_level":1011,
            "grnd_level":1008,
            "humidity":64,
            "temp_kf":0
         },
         "weather":[
            {
               "id":804,
               "main":"Clouds",
               "description":"awan mendung",
               "icon":"04d"
            }
         ],
         "clouds":{
            "all":100
         },
         "wind":{
            "speed":0.93,
            "deg":142,
            "gust":1.98
         },
         "visibility":10000,
         "pop":0,
         "sys":{
            "pod":"d"
         },
         "dt_txt":"2021-06-08 03:00:00"
      },
      {
         "dt":1623132000,
         "main":{
            "temp":304.02,
            "feels_like":307.02,
            "temp_min":304.02,
            "temp_max":304.02,
            "pressure":1009,
            "sea_level":1009,
            "grnd_level":1007,
            "humidity":57,
            "temp_kf":0
         },
         "weather":[
            {
               "id":804,
               "main":"Clouds",
               "description":"awan mendung",
               "icon":"04d"
            }
         ],
         "clouds":{
            "all":100
         },
         "wind":{
            "speed":1.19,
            "deg":68,
            "gust":1.58
         },
         "visibility":10000,
         "pop":0,
         "sys":{
            "pod":"d"
         },
         "dt_txt":"2021-06-08 06:00:00"
      },
      {
         "dt":1623142800,
         "main":{
            "temp":304.15,
            "feels_like":308,
            "temp_min":304.15,
            "temp_max":304.15,
            "pressure":1007,
            "sea_level":1007,
            "grnd_level":1004,
            "humidity":60,
            "temp_kf":0
         },
         "weather":[
            {
               "id":803,
               "main":"Clouds",
               "description":"awan pecah",
               "icon":"04d"
            }
         ],
         "clouds":{
            "all":58
         },
         "wind":{
            "speed":2.67,
            "deg":51,
            "gust":2.18
         },
         "visibility":10000,
         "pop":0.03,
         "sys":{
            "pod":"d"
         },
         "dt_txt":"2021-06-08 09:00:00"
      },
      {
         "dt":1623153600,
         "main":{
            "temp":298.71,
            "feels_like":299.65,
            "temp_min":298.71,
            "temp_max":298.71,
            "pressure":1010,
            "sea_level":1010,
            "grnd_level":1007,
            "humidity":89,
            "temp_kf":0
         },
         "weather":[
            {
               "id":500,
               "main":"Rain",
               "description":"hujan rintik-rintik",
               "icon":"10n"
            }
         ],
         "clouds":{
            "all":62
         },
         "wind":{
            "speed":0.69,
            "deg":45,
            "gust":1.05
         },
         "visibility":10000,
         "pop":0.3,
         "rain":{
            "3h":0.55
         },
         "sys":{
            "pod":"n"
         },
         "dt_txt":"2021-06-08 12:00:00"
      },
      {
         "dt":1623164400,
         "main":{
            "temp":297.75,
            "feels_like":298.69,
            "temp_min":297.75,
            "temp_max":297.75,
            "pressure":1012,
            "sea_level":1012,
            "grnd_level":1009,
            "humidity":93,
            "temp_kf":0
         },
         "weather":[
            {
               "id":500,
               "main":"Rain",
               "description":"hujan rintik-rintik",
               "icon":"10n"
            }
         ],
         "clouds":{
            "all":100
         },
         "wind":{
            "speed":1.47,
            "deg":244,
            "gust":1.54
         },
         "visibility":10000,
         "pop":0.61,
         "rain":{
            "3h":0.38
         },
         "sys":{
            "pod":"n"
         },
         "dt_txt":"2021-06-08 15:00:00"
      }
   ],
   "city":{
      "id":1214520,
      "name":"Kota Medan",
      "coord":{
         "lat":3.5833,
         "lon":98.6667
      },
      "country":"ID",
      "population":0,
      "timezone":25200,
      "sunrise":1623021281,
      "sunset":1623065636
   }
}

提前致谢。

标签: javascriptjson

解决方案


假设data是你的 JS 对象

let filtered = []; 
for (var row of data.list) {
    if (row.dt_txt.endsWith('00:00:00')) {
        filtered.push(row)  
    }
}

这将创建一个新数组,遍历您的值列表,如果dt_text以 00:00:00 结尾,则当前行将添加到您的filtered数组中


推荐阅读