Anglebaby is the only one who can blog on the last day of the year. You may be confused, there is a summary at the end of the article, take it

A: the for

1. Break:

var arr = [1.2.3];
var len = arr.length;

// for break
for(var i = 0; i < len; i += 1) {for(var j = 0; j < 3; j++){
        if(j === 1) {break;
        }
        console.log(arr[i]+The '-'+j); }}Copy the code




for break

🙂 if no label is added, break exits the current loop, that is, the second loop.

2. Continue:

var arr = [1.2.3];
var len = arr.length;

// for continue
for(var i = 0; i < len; i += 1) {for(var j = 0; j < 3; j++){
        if(j === 1) {continue;
        }
        console.log(arr[i]+The '-'+j); }}Copy the code




for continue

If no label is added, continue stops the current loop and continues the next loop (both for layer 2).

2: forEach

1. Return false:

var arr = [1.2.3];

arr.forEach(function(value,index) {
    if(index= = =1) {return false;
    }
    console.log(arr[index])});Copy the code




forEach return false

1. Return true:

var arr = [1.2.3];

arr.forEach(function(value,index) {
    if(index= = =1) {return true;
    }
    console.log(arr[index])});Copy the code




forEach return true

🙂 the end of this cycle, into the next cycle tips: forEach loop must be in break | | continue!

Three: Array. The map

Map is similar to forEach except that map returns an array

Four: the for… in

1. Break | | continue

var arr = [1.2.3];

for(var i in arr){
    if(i === 1) {break
    }
    console.log(i)
}Copy the code




for… in break

:)for… In break or continue has no effect on traversal, an error will be reported when there is a return!

Five: the for… of

1. Break

var arr = [1.2.3];

for(var i of arr){
    if(i === 2) {break
    }
    console.log(i)
}Copy the code




for… of break

:)for… Of breaks out of the loop, similar to for, except that I is the value of arr and not index 1.continue

var arr = [1.2.3];

for(var i of arr){
    if(i === 2) {continue
    }
    console.log(i)
}Copy the code




for… of continue

:)for… Of completes this loop, like for, tips:for… No return in the “of” loop! The first argument is the value

Six: $. The each ()

1. Return false

var arr = [1.2.3];

$.each(arr,function(index,value){
    if(index === 1) {return false;
    }
    console.log(value);
})Copy the code




$.each return false

🙂 out of this loop! 1. Return true

var arr = [1.2.3];

$.each(arr,function(index,value){
    if(index === 1) {return true;
    }
    console.log(value);
})Copy the code




$.each return true

🙂 end this loop! Tips: $. Each loop must be in break | | continue!

Grandpa, take a seat upstairs. Oh, no, it’s a mess. Are you dizzy, don’t worry, I write write also dizzy, hereby terminate:

  • (1) for: when there is no label, break breaks out of the loop and executes the code after the loop body, and continue ends the loop and executes the next loop. There is no return.
  • (2) array. forEach: iterate through the Array. Return false or true to terminate the loop and execute the next loop. There is no break | | continue.
  • (3) array. map: Similar to forEach, map has a return value. The return result is an Array of return values.
  • (4) for… In: ignore break | | continue. There is no return.
  • (5) for… Of: break breaks out of the loop and executes the code after the loop body. Continue terminates the loop and executes the next loop, just like for. Note:for(var v in arr)V is an array value! .
  • (6) $. Each: return false; Return true Completes the loop to execute the next loop. There is no break | | continue.

Abyss gave us the eyes of hatred, I will use it to see through the end of the darkness – Wei Zhuang