Please check if you can help anyone. I have been working on my weather app which I am building on React using axios with openweathermap.org and I am kind of stuck to get data the way I need it. I am using its 5day forecast. It gives you a report with every 3 hours forecast for 5 days.It gives you 40 reports for 5 days. so you get 8 reports for the 3 days in between and for current and the fifth day you get the remaining based on how much time left for current day. This is the API response for the report: report. So you will get this as the reponse:
{
"data": {
"cod": "200",
"message": 0.0062,
"cnt": 39,
"list": [
{
"dt": 1540177200,
"main": {
"temp": 24.55,
"temp_min": 20.88,
"temp_max": 24.55,
"pressure": 1008.67,
"sea_level": 1025.96,
"grnd_level": 1008.67,
"humidity": 58,
"temp_kf": 3.67
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "02d"
}
],
"clouds": {
"all": 8
},
"wind": {
"speed": 3.82,
"deg": 340.5
},
"sys": {
"pod": "d"
},
"dt_txt": "2018-10-22 03:00:00"
},
{
"dt": 1540188000,
"main": {
...
},
},
{
"dt": 1540198800,
"main": {
...
},
},
{
"dt": 1540587600,
"main": {
. . .
}
}
]
}
I just summarised it.I just put in the whole data for the first item and for the rest I put in ... and it is a long array with 40 items. Each one has a unique date timestamp which is "dt" in the beginning of it. I need to get 5 arrays based on a specific day. for that I thought of converting the timestamp(dt) and then get all the items that result in the same "Day". Convert the timestamp like this (using forEach for all items):
let day ='';
day = new Date(dt *1000);
day = day.getDate();
There are two problems when I get converted array:
- I get one big array with 40 items again but I need 5 arrays based on the date so that I have the report of each day seperate
- Secondly I loose the dt in timestamp. I need to keep that so that I can show the weather forecast in my application.
I want to show the info based on that timestamp for 5 days seperate.
Thanks for your help and ideas everyone.