JavaScript for and forEach end current cycle/end loop


foreword

I have always wanted to try this forembedding before switch, but I couldn’t find a suitable opportunity. I just encountered writing a node script today, so I must give it a try.


One, for

1. Terminate the current round

I put the correct way of writing here first, if the loop does not reach any number among 1, 2, 5, then this number will not be output, that is, it will not be executed console.log(arr[i]);.

function init() {
    
    
  for (let i = 0; i < arr.length; i++) {
    
    
    switch (arr[i]) {
    
    
      case 1:
      case 2:
      case 5:
        console.log('');
        break;
      default:
        continue;
    }
    console.log(arr[i]);
  }
}

const arr = [1, 2, 3, 4, 5, 6];

init();

At first I wrote it like this:

function init() {
    
    
  for (let i = 0; i < arr.length; i++) {
    
    
    switch (arr[i]) {
    
    
      case 1:
      case 2:
      case 3:
        console.log('');
        break;
      default:
        return;
    }
    console.log(arr[i]);
  }
}

const arr = [1, 2, 3, 4, 5, 6];

init();

The result seems to be correct, 123, but I soon realized that it may be that the cycle did not go on after reaching 4, and the two numbers after 4 were not judged at all? So I did not make the
filter number of the switch so smooth, Jump to see if the execution is terminated:

function init() {
    
    
  for (let i = 0; i < arr.length; i++) {
    
    
    switch (arr[i]) {
    
    
      case 1:
      case 2:
      case 5:
        console.log('');
        break;
      default:
        return;
    }
    console.log(arr[i]);
  }
}

const arr = [1, 2, 3, 4, 5, 6];

init();

If 5 is not output, it means that the execution will stop directly after reaching 3.

insert image description here

Sure enough, there is a problem. returnThe entire function is returned directly, and forthe following things are not executed.
Then a syntax that can only skip the current round of the for loop is needed as defaulta solution.
continueIn forit, the current loop is skipped.

By the way, it is used above break, but breakthe outer layer is first switch, so the loop will continue. If there is no such layer and it is used switchdirectly inside , the loop will be terminated directly . Refer to the following example.forbreakfor


2. Terminate the loop

Let's reproduce the situation discussed at the end of the previous section:

function init() {
    
    
  for (let i = 0; i < arr.length; i++) {
    
    
    switch (arr[i]) {
    
    
      case 1:
      case 2:
      case 5:
        console.log('');
        break;
      default:
        return;
    }
    console.log(arr[i]);
    break;
  }
}

const arr = [1, 2, 3, 4, 5, 6];

init();

According to me, only one 1 should be output at this time, because the loop terminates directly after the first round of loop output:

insert image description here

right.

for...inThe end and skip loop methods are the same as above.


Two, forEach

1. Terminate the current round

Terminating this round is very simple, you only need to click forEachin returnto end this round.

function init() {
    
    
  arr.forEach((item) => {
    
    
    if (item === 1) return;
    console.log(item);
  })
  console.log('www');
}

const arr = [1, 2, 3, 4, 5, 6];

init();

This will output 23456.
But you can see that this does not forEachend up with the whole, so there should be no digital output.


2. Terminate the loop

You can choose to terminate by throwing an error forEach, but there's no way to end it nicely and peacefully.
MDN:

insert image description here


Summarize

Finally, the reading volume is 300,000, thank you for your support over the past two years, you have given me a lot of spiritual support, let me go on this road, and I will continue to go on.

Guess you like

Origin blog.csdn.net/qq_52697994/article/details/130858603