I've been using the weather api forecast for 5 days/3hrs and I'm stuck trying to group together data of the same day, and then getting maximum and minimum temp for each day. for example, I'd like to group all the data for Saturday into one object,
object ={
{day: 'Sat Mon dd', temp: [array of temp every 3 hrs on Sat], icon: [array of icons for each temp]},
{day: 'Sun Mon dd', temp: [array of temp every 3 hrs on Sat], icon: [array of icons for each temp]}, ...}
Is there a way to do that? or do you have a better idea on how to achieve this? I am a beginner.
also, I don't have my mind set on getting a max and min temp for a day, or their icon. I just think it wouldn't be realistic to get one avg temp each day. all ideas are welcome, and thanks!
my code and and what I managed to get is below.
fetch(
`${apiForecastUrl}q=${inputForecast}&appid=${apiKey}&units=metric`
)
.then((response) => response.json())
.then((data) => {
let forecastObjArr = [];
for (let i = 0; i < data.list.length; i++) {
let forecastDay = new Date(data.list[i].dt * 1000).toString().slice(0, 24);
let forecastTemp = data.list[i].main.temp;
let forecastIcon = data.list[i].weather[0].icon;
let forecastObj = {
day: forecastDay,
temp: forecastTemp,
icon: forecastIcon,
};
forecastObjArr.push(forecastObj);
}
console.log(forecastObjArr);
});