Angular foreach method encounters a syntax issue

When I use the getTotal.getValues() function to make a server call that returns values like "one", "two", "three" up to "nine", I am able to print them using console.log(res). However, I am facing an issue where I cannot push these returned values into the variable v that I created inside the runTest function. The problem is that when I run the code, console.log(r) does not display anything because the return v is empty. Any suggestions on how to resolve this?

var test = [1,2,3,4,5,6,7,8,9];

function runTest(val) {
    var v = [];
    val.forEach(function(t) {
        getTotal.getValues(t).then(function(res) {
            //console.log(res);
            v.push(res);
        });
    });
    return v;
}
runTest(test).forEach(function(r) {
    console.log(r);
});

Answer №1

Confused between Angular's forEach and JavaScript's forEach?

Make sure you include Angular's forEach in your code snippet.

angular.forEach(list, function(item) {
    console.log(item);
});

Answer №2

Below is an example of how your forEach function should be structured:

var example = [10,20,30,40,50];
example.forEach(function outputArrayValues(value, index, array) {
    console.log("array[" + index + "] = " + value);
});
// outputs:
// array[0] = 10
// array[1] = 20
// array[2] = 30

More information on Array.forEach()

If you prefer using angular.forEach(), you can check out: angular.forEach documentation

Best of luck!

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

Looking up results in an array in PHP/MySQL

I have retrieved the results from mysql ($date_options) as shown below: Array ( [0] => stdClass Object ( [id] => 4 [start_date] => 2010-09-29 ) [1] => stdClass Object ( [id] => 13 [start_date] =&g ...

Issues with zoom functionality not functioning properly within IE11

I am currently developing an application with Angular that is designed to be compatible with tablets and touch-enabled devices. One of the key features I want to implement is the ability for users to zoom/scale up the app, especially for those with visual ...

Is it necessary to have both Express and express-generator in my toolbox?

Recently delving into the world of node.js and stumbled upon express; While browsing the npm repository site https://www.npmjs.com/package/express, I noticed that the installation process is clearly stated as: $ npm install express However, further down ...

The document.getElementsByClassName method in JavaScript is returning an Array with a length property value of 0

My HTML contains a list of li elements with one class. I am attempting to collect them all and place them in an array to determine how many have been gathered. However, when I attempt to display the number using the .length property of the array returned b ...

Implementing a div element within an autosuggest feature

i am currently integrating the bsn autosuggest into my project could someone please guide me on how to insert a div in the result so that it appears like this <div style="left: 347px; top: 1024px; width: 400px;" class="autosuggest" id="as_testinput_x ...

What is the best way to initiate a TouchEvent in a qunit test being run by grunt using only vanilla JavaScript?

I have implemented callbacks for different touch events that require testing. For example, the 'touchstart' event utilizes touch coordinates to configure a class member: NavigationUI.prototype.touchStart = function(evt) { this.interacting = ...

Saving URLSearchParams to a file using javascript

I am currently implementing jstree and I need to be able to click on a file within the tree structure in order to display a PDF file. Below is the relevant code snippet: $(function () { $('#tree').jstree({ 'core' : { ...

What is preventing me from retrieving the attribute value from my custom directive?

The angularjs code: app.directive('test', function(){ return { restrict: 'A', scope: { myId: '@' }, link: function(scope) { alert(scope.myId); } } }); In the directive, there is a scope: { ...

What techniques can I use to adjust the size of an image through zooming in and out?

In my custom gallery component, the crucial code section looks like this: <Gallery> <Header> <img src={galleryIcon} alt='Galley icon' /> <h1>My Gallery</h1> </Header> ...

The useRouter hook from next/router was unable to connect because NextRouter was not activated

import { useRouter } from 'next/router'; const Navbar2 = () => { const router = useRouter(); return ( <nav className={`fixed top-0 w-full px-10 bg-white p-4 transition-all duration-500 ${isVisible ? 'top-0' : 'top-[-1 ...

Using React, we can create a component by defining it as a constant and then

Currently, I have a React component that is created as a const and takes props. In another file, there is a function called selectChanged() {} which returns undefined every time the select value is changed. The code for the component is as follows: ... ...

What steps should be taken to address the issue when the .media$thumbnail.url cannot be located

Creating a custom widget for bloggers <script type="text/javascript"> function mycallback(jsonData) { for (var i = 0; i < jsonData.feed.entry.length; i++) { for (var j = 0; j < jsonData.feed.entry[i].link.length; j++) { ...

Passport verification complete but fails to redirect, displaying a 500 error instead

Currently, I am deepening my understanding of the mean stack through the development of a social media web application similar to Twitter. However, during the signup or login process, everything seems to be working smoothly as the user is successfully crea ...

Executing a function with a click, then undoing it with a second click

My goal is to trigger an animation that involves text sliding off the screen only when the burger icon is clicked, rather than loading immediately upon refreshing the page. The desired behavior includes activating the function on the initial click and then ...

Implementing V-model within an iteration

I am encountering an issue while trying to utilize v-model within a v-for loop, resulting in an error message. Is there a solution to make this functionality work properly? <ul class=""> <li class="" v-model="category.data" v-for="category in ...

Is there a way to format this into four columns within a single row while ensuring it is responsive?

I am working on a layout with a Grid and Card, aiming to have 4 columns in one row. However, the 4th column ends up in the 2nd row instead, as shown below: https://i.sstatic.net/gOeMm.png My goal is to align all 4 columns in a single row. Additionally, w ...

The Promise.all function encountered an error: Uncaught TypeError: #<Promise> is not an iterable object

Currently, I am dealing with the challenge of hitting two APIs and waiting for both responses to return before dispatching my action. Although I am utilizing Promise.all, I am encountering the following error: index.js:51 Uncaught (in promise) TypeErro ...

Achieving synchronization within the 'it' block using Protractor

I am currently experiencing synchronization issues in the 'it' block of my code. The following snippet illustrates the problem: it('Some Download Operation',function() { console.log("before"); myobj.clickOnDownloadBtn(); ...

Adjustable value range slider in HTML5 with ng-repeat directive in AngularJs

I am facing a problem with my HTML5 range slider. After setting a value (status) and sending it to the database, when I reload the page the slider's value is always set to '50'. The slider is being generated within an ng-repeat from AngularJ ...

Error in refreshing JWPlayer page: Plugin loading failure - File not located

I'm implementing JWPlayer on my Localhost environment. Here's the code snippet I'm using: <div id="Player">loading...</div> <script type="text/javascript"> jwplayer("Player").setup({ file: "<?php echo $video[' ...