Is there a way to iterate over the data from the returned Resource object using an angular.forEach loop in the controller

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.

https://i.sstatic.net/e5YkU.jpg

Answer №1

$scope.temperatures = [];
angular.forEach(data[0].temperatures, function(value, key){
  $scope.temperatures.push(value);
})

Give this a try and see if it works for you.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Every time I refresh the app, I am consistently redirected back to the home route "//"

I'm facing an issue where, after logging in successfully, I get redirected to the homepage. However, when I refresh the page from any route other than the homepage, such as "/products", I always end up getting redirected back to "/". This is what my ...

Initiate gapi.auth2 upon VueJs initialization

I am currently developing a Vue.js web application that allows users to connect with their Google accounts. The login process, both front-end and back-end, is functioning properly: the Google sign-in button is displayed, the user clicks on it, and their a ...

Can anyone provide guidance on uploading data to a mongodb database? I've attempted a few methods but keep encountering errors

const info = { name, price, quantity, image, desc, sup_name, email } fetch('https://gentle-plateau-90897.herokuapp.com/fruits', { method: 'POST', headers: { 'content-type': 'application/jso ...

Encode a variable into base64 using the Buffer module in node.js

Attempting to convert a variable from an HTTP parameter to base64 using node.js and Buffer. Code snippet: var http = require("http"); var url = require("url"); http.createServer(function(req, res) { var parsedUrl = url.parse(req.url, true); var que ...

Sending Dual Parameters to PHP File Using AJAX

Currently, I am facing an issue where I can successfully pass one value to a Bootstrap modal via AJAX, but my code stops working when I try to pass a second value. JavaScript function claimOrder(str, stre){ if (str=="") { document.getElementById("txtH"). ...

Is there a way to incorporate locales in calculations involving percentages?

Given the number 4030.146852312 I want to retrieve the four decimal places from this number, resulting in: 4030.1468 Furthermore, I need to format this number according to the specified locale. For example: 4.030,1468 What is the best way to achieve thi ...

What is the best way to adjust the size of an SVG path when zooming in Highcharts?

Recently, I developed a module that allows users to draw an svg line over a highchart using the Renderer. However, I encountered an issue where the size and position of the drawn line remain static when the user zooms into the highchart. It is essentia ...

What is limiting me from utilizing the entire Google Calendar feed?

I currently have a public Google Calendar set up. My goal is to retrieve appointment data in JSON format from this calendar. When I utilize the following URL https://www.google.com/calendar/feeds/{calendar_id}%40group.calendar.google.com/public/basic?alt ...

"Before the click even happens, the jquery click event is already

As I was developing a small todo app, I ran into an issue where the alert message seemed to pop up before the click event was triggered. It almost seems like the event is not registered properly. Can someone explain why this is happening? (function() { ...

Vue.js - The table updates successfully, however, the data for the selected checkboxes remains unchanged

Encountered a persistent bug that is giving me some trouble. I have a table with rows that can be selected, and when a checkbox is checked, the total amount in the column should be calculated. However, whenever there is a change in the table data through ...

The React component is failing to display updated data from the Redux store

I've been working with React and Redux, trying to update my counter value in the React view. I can successfully log the latest state of my Redux store, but the changes are not reflecting in my React view. const counter = (state = 0, action) => { ...

Speedy method for declaring array elements incrementally - Java

Assume I have 10 integer variables, named x1 to x10. Defined below is an integer array: Int[] countup = new Int[10]; I wish to assign values to the array elements in the following manner: countup[0] = x1; countup[1] = x1 + x2; countup[2] = x1 + x2 + x3 ...

The method by which npmjs.com evaluates the quality of code

After we release a package on npm, it provides us with insights such as popularity, quality, and maintenance on the search page (Refer to the example image below). I am particularly curious about how npm determines the quality metric. Any insights would be ...

Angular 2 ngFor generates a collection of rows and columns forming a single large column

It seems that ngfor is generating divs one by one, resulting in a poor design where they are stacked on top of each other. I would like to achieve a layout like this: [1] [2] [3] [4] [5] [6] However, the current outcome looks like this: [ 1 ] [ 2 ] [ 3 ...

Converting JSON information into a JavaScript array

var test=[]; $(document).ready(function(){ $.getJSON("data.json",function(data){ $.each(data,function(key,value){ test.push(value.topic); }); }); }); I have written some j ...

What is the best way to extract information from the response of a fetch request in JavaScript using Express and Node.js

Currently, my script code resides in the index.html file, which I understand is not the ideal location. However, I am in the process of getting it to work before relocating it. readOperaciones(); async function readOperaciones(){ try{ ...

Tips for managing the response from a POST request using jQuery

I'm currently working on sending data via POST to my ASP.Net MVC Web API controller and retrieving it in the response. Below is the script I have for the post: $('#recordUser').click(function () { $.ajax({ type: 'POST', ...

struggling to retrieve JSONP information from my personal API using AngularJS

I have been attempting to retrieve data from my own API, but unfortunately, I have encountered difficulties in storing it in the vegdata variable. Below is the controller code: $scope.filterText = null; $scope.vegdata =[]; $scope.init = function() { u ...

What could be causing my HTML5 page to crash when accessed online, but not when viewed locally?

I am just getting started with HTML5 and decided to experiment with canvas. This is the first canvas page I have created. Everything works fine when I run the page locally (i.e. file:///), but once I upload the files to my webhost, the page gets stuck whi ...

The readFileSync function is unable to locate the file using the provided URL

When attempting to download and convert a file without saving it, I encountered an error with readFileSync indicating the file cannot be found: "Error: ENOENT: no such file or directory, open 'https://s3.amazonaws.com/appforest_uf/f1631452514756x ...