While browsing online, I encountered a similar issue. Despite my efforts, all I kept receiving was undefined errors in the console.
The data is being fetched from the openweathermap API using the angular $resource factory. I am aware that I can extract the necessary information in the view using ng-repeat, but I also want to break down and access specific key-value pairs in the controller. When I
console.log($scope.weatherResult)
, which contains the retrieved data, I see a Resource object in the console.
How can I loop through this object to extract the required data? The structure consists of objects nested within other objects and arrays. While ng-repeat works well in the view to display the data, I am unsure how to access it in the controller. For instance, when I do a
console.log($scope.weatherResult.city)
or console.log($scope.weatherResult.list)
, the output is always undefined.
The data I need to extract resides within the list array - specifically two days' worth of data. The initial object list[0].temp contains temperature values; I aim to calculate their sum, determine the average, store it in another variable in the controller, and ultimately display it on the view.
I am uncertain if my approach is overly complicated. In previous cases, I managed similar situations by utilizing ng-repeat in the view. However, I am eager to learn how to achieve this in a more efficient manner.
Is it feasible to iterate through the resource object using the angular.forEach()
function to retrieve data within the temp object? I have included an image for reference. My knowledge and experience with JavaScript and angular are still growing.