I have a JavaScript program where I retrieve values from a JSON file and store them in an array. However, when I attempt to access the elements of this array, it returns nothing.
Below is the function that pushes temperatures:
temperatures = []
get_info = (data) => {
var data = JSON.parse(data)
for(var i = 0; i < data['list'].length; i++){
temperatures.push(String(data['list'][i]['main']['temp']))
}
}
Here is how I fetch the data and call the function:
weather = (city_name) => {
var key = '.......';
var base_url = 'http://api.openweathermap.org/data/2.5/forecast?';
var url = base_url + 'appid=' + key + '&q=' + city_name + '&units=metric';
fetch(url)
.then(response => response.text())
.then(contents => get_info(contents))
}
When logging temperatures
, it shows an empty array []
.
How can I correctly access the elements of the temperatures array?