1

I have a each loop inside a IF condition, inside this loop again I have IF ELSE condition, if IF condition inside this each loop matches I want my compiler to jump out of the outer IF condition inside which I have this each loop,

If I use return or return false it throws me outside of each loop, not outside the outer IF condition.

if (localStorage.getItem('Token')) {
            $(".menu-highlight li a").each(function () {
                var aTag= $(this).text();
                if(route==aTag){
                    this.router.navigate([route]);
                }
                else{
                    return;
                }
            });
           alert('User is already logged In');
}

I want compiler to jump out of this IF (if (localStorage.getItem('Token')) {}) condition

AshuTomar
  • 29
  • 1
  • 4
  • 3
    A sample snippet will help in explaining what you mean.. – Suraj Rao Feb 06 '17 at 06:31
  • 2
    could you provide some sample code. It'll be easier for the viewers to understand for future reference. – Manish Poduval Feb 06 '17 at 06:31
  • try to use continue, and the jump directly to the next iteration. – Álvaro Touzón Feb 06 '17 at 06:32
  • Put you code snippet as well – Mayank Pandeyz Feb 06 '17 at 06:32
  • Possible duplicate of [How to short circuit Array.forEach like calling break?](http://stackoverflow.com/questions/2641347/how-to-short-circuit-array-foreach-like-calling-break) – Kos Feb 06 '17 at 06:36
  • @Kos Probably not, tagged with jQuery and containing "_each loop_" means likely `$.each()`? – Teemu Feb 06 '17 at 06:39
  • 1
    Then it might be a duplicate of [How to break out of jQuery each Loop](http://stackoverflow.com/questions/1784780/how-to-break-out-of-jquery-each-loop) – Kos Feb 06 '17 at 06:42
  • Exit the outer `if` condition? What does that actually mean? Also having `each` loop within an _`if` condition_ doesn't make much sense, since `each` always returns the iterated object. – Teemu Feb 06 '17 at 06:58

1 Answers1

0

There's no direct way of doing it with each (If forin is used this could have been easy). Try following way of adding a custom variable(breakTheLoop, in my example) for identifying and skipping execution inside each loop.

Edit: Array 1, I break out of foreach and reset of the code inside outer if condition. Array 2, Condition is not broaken, of forloop completes and reset of code in outer If condition is executed.

var array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
        var array2 = [1, 2, 3, 4, 5];

        console.log("Array 1");
        sampleFunction(array1);


        console.log("Array 2")
        sampleFunction(array2);

        function sampleFunction(intArray) {
            var someFlag = true;
            var breakTheLoop = false;

            if (someFlag == true) {//First If Condition
                console.log("Outer IF Condition");

                for (var i = 0; i < intArray.length; i++) {
                    if ($(this)[0] > 5) {//Inner IF Else Condition
                        console.log("Reached 6, break the loop");
                        breakTheLoop = true;
                        break;  //Flag to indicate to skip forach execution
                    }
                    else {
                        console.log("Less than 6 : Value - " + intArray[0]);
                    }
                }

                if (!breakTheLoop) {
                    console.log("Outer IF, After For - 1");
                    console.log("Outer IF, After For - 2");
                    console.log("Outer IF, After For - 3");
                }
            }
        }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Shiva K Thogiti
  • 208
  • 2
  • 6