[Solved ]- AngularJS – How to work with break, ForEach?

How to work with break, ForEach?

Asked on October 16, 2018 in AngularJs.
Add Comment


  • 6 Answer(s)

    There’s no real way to do this. Contingent upon what you’re doing you can utilize a boolean to simply not going into the body of the circle. Something like:

    var keepGoing = true;
    angular.forEach([0,1,2], function(count){
    if(keepGoing) {
    if(count == 1){
    keepGoing = false;
    }
    }
    });
    

     

    Answered on October 16, 2018.
    Add Comment
    Array.prototype.some:
    
    

    Some is much the same as forEach but it break when the callback returns true

    Array.prototype.every:
    
    

    Every is almost identical to some except it’s expecting false to break the loop.
    Example for some:

    var ary = ["JavaScript", "Java", "CoffeeScript", "TypeScript"];
    ary.some(function (value, index, _ary) {
    
    console.log(index + ": " + value);
    
    return value === "JavaScript";
    
    });
    

    Example for every:

    var ary = ["JavaScript", "Java", "CoffeeScript", "TypeScript"];
    ary.every(function(value, index, _ary) {
    
    console.log(index + ": " + value);
    
    return value.indexOf("Script") > -1;
    
    });
    
    Answered on October 16, 2018.
    Add Comment

    To the extent I know, Angular doesn’t give such a capacity. You might need to utilize underscore’s find() work for this present (it’s essentially a forEach which breaks unaware of what’s going on once the capacity returns genuine).

    Answered on October 16, 2018.
    Add Comment

    There is no way to stop or break a forEach() loop other than by throwing an exception. If you need such behaviour, the .forEach() method is the wrong tool, use a plain loop instead. If you are testing the array elements for a predicate and need a boolean return value, you can use every() or some() instead.

    Array.prototype.some() executes the callback function once for each element present in the array until it finds one where callback returns a truthy value (a value that becomes true when converted to a Boolean). If such an element is found, some() immediately returns true. Otherwise, some() returns false. callback is invoked only for indexes of the array which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values.

     

    Answered on December 22, 2018.
    Add Comment

    The angular.forEach loop can’t break on a condition match.

    My personal advice is to use a NATIVE FOR loop instead of angular.forEach.

    The NATIVE FOR loop is around 90% faster then other for loops.

    For loop break , for loop test result

    USE FOR loop IN ANGULAR:

    var numbers = [0, 1, 2, 3, 4, 5];

    for (var i = 0, len = numbers.length; i < len; i++) {
    if (numbers[i] === 1) {
    console.log('Loop is going to break.');
    break;
    }
    console.log('Loop will continue.');
    }

    Answered on February 5, 2019.
    Add Comment

    There’s no way to do this. See https://github.com/angular/angular.js/issues/263. Depending on what you’re doing you can use a boolean to just not going into the body of the loop. Something like:

    var keepGoing = true;
    angular.forEach([0,1,2], function(count){
      if(keepGoing) {
        if(count == 1){
          keepGoing = false;
        }
      }
    });
    Answered on February 9, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.