I'm creating a JSON object in NodeJS which will be returned in a GET call. This object must return weather forecast for 3 hours period of each day from the current day.
The JSON desired structure will be:
Desired response
{
"current": {
// Stuff... This part works fine
},
"forecast": [
"Tuesday": [
{
// More stuff...
},
{
// More stuff...
},
// More stuff...
],
"Wednesday": [
{
// More stuff...
},
{
// More stuff...
},
// More stuff...
],
// More stuff...
]
}
In NodeJS I do:
json.forecast = [];
json.forecast[weekDay] = [];
for (var i = 1; i < data.cnt; i++) {
var hour = {};
var forecastDay = getDay(forecast[i].dt_txt);
if (forecastDay !== weekDay) {
weekDay = forecastDay;
json.forecast[weekDay] = [];
}
hour.date = forecast[i].dt_txt || 'unknow';
hour.day = getDay(forecast[i].dt_txt);
hour.time = getTime(forecast[i].dt_txt);
hour.temp = getValue(forecast[i].main.temp);
hour.maxtemp = getValue(forecast[i].main.temp_max);
hour.mintemp = getValue(forecast[i].main.temp_min);
hour.wind_speed = getValue(forecast[i].wind.speed);
hour.rain = forecast[i].rain ? forecast[i].rain["3h"] : 'unknow';
hour.humidity = getValue(forecast[i].main.humidity);
hour.condition = {};
hour.condition.text = forecast[i].weather[0].description || 'unknow';
hour.condition.icon = getIconUrl('openweather', forecast[i].weather[0].icon, logger);
json.forecast[weekDay].push(hour);
}
The var "weekDay" is generic because I don't know in which day I will start and because I want to create a new item for each day in runtime.
The problem is that every day item pushes into the array but length isn't growing:
And the GET response returns an empty array: