0

i know the question has been asked many times. but im not getting it done. can some one help in to exit from inner loop only. break labelname not working for me.

loop1:
    for (var i = 0; i < timeHolder.length; i++) {
    loop2:
        for (var key2 in self.TimeHolder[0]) {

            if (self.TimeHolder[0].hasOwnProperty(key2)) {
                if (self.TimeHolder[0][key2] == timeHolder[i]) {
                    var pattern = _.pick(row, key2)[key2];
                    var status = pattern.split('-');

                    $('#tblMatrix tr:last').append(self.createCompanyStatusRowsByRow(status));
                  break loop2;
                }
            }
        }
    }

however break loop2 exits from outer loop also.

2
  • 1
    why not simply give break; instead of break loop2; Commented Jan 18, 2013 at 8:54
  • Are you certain that when the break statement happens, the outer loop should execute again? Maybe it's just exiting because i >= timeHolder.length Commented Jan 18, 2013 at 8:56

3 Answers 3

4

Take a look at this question: Best way to break from nested loops in Javascript? Best answer (I think) is:

loop1:
    for (var i in set1) {
loop2:
        for (var j in set2) {
loop3:
            for (var k in set3) {
                break loop2;  // breaks out of loop3 and loop2
            }
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

1

Just use break instead of break loop2.

The break keyword will break you out of the current executing loop.

Comments

0

A simple break works perfectly for me.

var elems = [ "a","b","c","d","e" ];
for( var i = 0; i < 5; i++ ) {

  console.log( i );

  for( var j in els ) {

    if( elems[j] == "c" ) {

      break;
    }

    console.log( " " + elems[j] );
  }
}

With expected output

0
 a
 b
1
 a
 b
2
 a
 b
3
 a
 b
4
 a
 b

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.