0

if suppose i have a programme like below:

for(var char in elem_text_ary) {
            //console.log(elem_text_ary[char].charAt(0));

            for(var desired_chars in desired_Chars){
                if(elem_text_ary[char].charAt(0) === desired_Chars[desired_chars].toUpperCase()) {
                     console.log(elem_text_ary[char].charAt(0));  
                     heading_arr.push( '<span>' + elem_text_ary[char].charAt(0) + '</span>' + elem_text_ary[char].slice(1 , elem_text_ary[char].length) );
                    console.log(heading_arr);
                    /*return;*/
                }  else {
                    heading_arr.push( elem_text_ary[char].slice(0 , elem_text_ary[char].length ));
                    /*return;*/
                }
            } // end for in loop
            //return false;
        } // end for in loop;

now see how there is two for in, now suppose i find what i want to in the 2nd for..in , I.E. if either the if or the else condition is met , how do i exit the internal for..in loop and tell the external for..in loop to continue ?

Thank you.

Alex-z.

Alexander Solonik
  • 9,838
  • 18
  • 76
  • 174

2 Answers2

1

Use break to interrupt an for-loop. This is a common keyword in almost every language.

Note that if you are considering interrupt a for-loop, then you can probably refactor the inner for-loop as a function call.

stanleyxu2005
  • 8,081
  • 14
  • 59
  • 94
1

You can use the break statement, to continue to the outer loop:

for(var char in elem_text_ary)
    for (var desired_chars in desired_Chars) {
        if (i === 3) { break; }
    }

However, you probably should not be using for in on an array

Community
  • 1
  • 1
Jaycee
  • 3,098
  • 22
  • 31
  • Hey buddy, you are over-explain the `break` `continue` thing. For a person, who have over 1k reputation, should already know that. – stanleyxu2005 Oct 09 '15 at 07:44