Is there a way to fully break out of a .forEach loop that's nested within a function?

Here is the method I am currently working with:

    wordFormDirty = (): boolean => {
    var self = this;
    angular.forEach(self.word.wordForms, function (wf, key) {
        var wordFormNgForm = 'wordFormNgForm_' + wf.wordFormId
        if (!self[wordFormNgForm].$pristine) {
            return true;
        }
    });
    return false;
};

I have noticed that this method never returns true. Any suggestions on how to modify it so that a form that is not pristine will cause the wordFormDirty() method to return true?

Answer №1

Do you want to give this a shot? In this scenario, if I'm understanding the issue correctly, when there is a true value for the first time, the result will be set to true; otherwise, it stays false.

wordFormDirty = (): boolean => {
    var self = this;
    var result = false;
    angular.forEach(self.word.wordForms, function (wf, key) {
        var wordFormNgForm = 'wordFormNgForm_' + wf.wordFormId
        if (!self[wordFormNgForm].$pristine) {
            result = true;
        }

    });
    return result;
};

Answer №2

For those looking to obtain a result by walking through the Array, it may be beneficial to explore alternative methods instead of relying solely on the forEach function. Here's an example:

const checkDirtyForms = Object.values(this.word.wordForms).some(
  ({ wordFormId }) => !this[`wordFormNgForm_${wordFormId}`].$pristine
);
return checkDirtyForms;

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

Instead of adding a new object to the array, consider increasing the quantity of an existing object

I am new to working with a shopping cart component and my code is structured like this: let cartCount = document.getElementById("counter"); let isItemSelected = false; let itemCount = 0; let shoppingCart = []; let selectedSize = ""; let displaySelectedSiz ...

Ways to show alternative data from a database in Laravel 8

I am working on a project where I need to display additional data based on the option selected from a dropdown menu populated from a database. Can anyone guide me on how to achieve this using javascript or jquery? https://i.stack.imgur.com/k3WLl.png Belo ...

Is Ajax still a popular choice for developing web applications?

Currently, I am developing a comprehensive ajax-based web application and have been investigating various frameworks for SEO optimization and history tracking. During my research, I came across an interesting framework at Although it seems promising, I no ...

Having issues with AngularJS routing functionality not functioning as expected

Currently, I am delving into AngularJS as part of a project I'm working on. Initially, AngularJS seemed like a promising framework, offering great support and user-friendly features. However, I have hit a roadblock when trying to implement routing... ...

Vue Dynamic Table Title

Is it possible to add labels to a pivot-table in Vue without affecting array indexes and drag-and-drop functionality as shown in the screenshot below? https://i.stack.imgur.com/5JTSM.png Are there alternative methods for implementing this feature? You c ...

Displaying a div in Vue.js when a button is clicked and an array is filled with

Hey there! I'm having an issue with displaying weather information for a specific location. I want to show the weather details in a div only when a button is clicked. Despite successfully fetching data from the API, I'm struggling to hide the wea ...

Headers cannot be set again after they have been sent to the client in Express Node

I attempted to create a post request for login with the following code: router.post('/login', async(req, res) =>{ const user = await User.findOne({gmail: req.body.gmail}) !user && res.status(404).json("user not matched") c ...

JQuery drag and drop functionality experiencing issues in Chrome Browser specifically when integrated into a Joomla Article

I recently embedded a jQuery drag and drop example into my Joomla article. Surprisingly, it works perfectly fine on Firefox browser but encounters issues on Chrome. Although the drag and drop functionality works on Chrome, the problem is that the buttons b ...

add the AJAX response to the specified div element

I'm currently using an ajax call to retrieve movie data from the IMDb API for 'The Shawshank Redemption'. My goal is to display this data within the designated div element. <div id="movie-data"></div> Here's my current Jav ...

Conditions in Controller Made Easy with AngularJS

I have recently started working on implementing a notifications feature. The service will involve making a GET request to a specific URL which will then return an array of notifications. Within the controller, I am in the process of setting up a variable ...

Tips for transferring a variable to another .php file using ajax

Is there a way to pass the id in posts.php using ajax while keeping the href value as "#"? Currently, when I click on this link, it redirects to that link. However, I do not want to navigate to the link and still need to pass the id in posts.php. Any sug ...

What is the best way to transfer a user-generated PNG file to my backend server?

I'm in the process of developing a website that enables users to generate personalized graphics and easily download them directly from the platform. My approach involves allowing users to customize an svg using a javascript-powered form, which is the ...

Eliminate a specific element from the dataset

My question revolves around a data array used to populate three drop down <select> boxes. How can I eliminate duplicate values within the drop downs without affecting the array itself? For example: data = { firstbox_a: ['grpA_1','gr ...

Using importNode in the context of Microsoft Edge involves transferring a

I am facing an issue with a dynamic page that has the ability to change its main div content using a bar button. The pages are mostly static except for one which contains JavaScript (RGraph charts). To make it work, I am currently using the following code ...

Encountering a 404 error with Angular 6 routing after refreshing the page when using an Nginx proxy

I currently have my angular application running within a docker container, exposed on port 83. Additionally, I have a separate spring-boot rest app running inside another docker container, exposed on port 8083. On the host server, there is an Nginx server ...

What is the best approach to display data in React fetched from an API request? If this is not the right method, what changes should be made to the JSX rendering to convert

As I begin my journey with React, I find myself questioning the best practices for displaying data. Should I always break down components into smaller ones rather than having one large component render everything? It seems like a good practice, but I' ...

What is the best way to retrieve the [id]/routeName in Next.js?

I'm working on a straightforward project where users visit links like: localhost:3000/course/[id]. Once they click join, the URL will transform to localhost:3000/course/[id]/routeName. How can I organize folders like that within pages? Thank you in ad ...

Encountering an issue following the installation and integration of an npm module within a Meteor

I recently started a project using the latest version of Meteor. I added a new package by running the command: meteor npm install --save name_of_package Since this package is meant for the client side, I created a file called mypackage.js inside the /cli ...

Replace the System package in mean.io

Q1) I am in need of assistance with overriding the System package in mean.io. My goal is to create a custom home page. This is what I have tried: angular.module('mean.mysystem', ['mean.system']) .config(['$viewPathProvider&apos ...

Is the purpose of express.json() to identify the Request Object as a JSON Object?

app.js const express = require('express'); const cookieParser = require('cookie-parser'); const session = require('express-session'); const helloRouter = require('./routes/hello'); const app = express(); app.set(& ...