How to jump out of loop/end traversal in JavaScript

Keywords: Javascript

Directly throw a conclusion. The following table is a jump/end traversal method commonly used in JS to implement loop traversal. It is summarized after testing.Maybe you guys have other ways to do that. I'm here to say the big guys NB.

Sequence Number Method break continue return return true return false conclusion
1 for loop Success Jump out of this cycle Wrongful Wrongful Wrongful
2 Array.forEach() Wrongful Wrongful Jump out of this cycle Jump out of this cycle Jump out of this cycle ×
3 for...in Success Jump out of this cycle Wrongful Wrongful Wrongful
4 Array.map() Wrongful Wrongful Jump out of this cycle Jump out of this cycle Jump out of this cycle ×
5 Array.some() Wrongful Wrongful Jump out of this cycle Success Jump out of this cycle
6 Array.every() Wrongful Wrongful Success Jump out of this cycle Success
7 Array.filter() Wrongful Wrongful Jump out of this cycle Jump out of this cycle Jump out of this cycle ×

For Each, map, and filter I don't know what I can do to stop traversing at this time. Among the other methods, the methods listed in the table above can end the loop.

1. for loop

var arr = ['a', 'b', 'c', 'd', 'e'];
var show = [];

for (var i = 0; i < arr.length; i++) {
    if (i === 2) {
        break;// ['a','b'] successfully jumped out of the loop
        // Continue; // ['a','b','d','e'] can only jump out of this cycle
        // return;// Uncaught SyntaxError: Illegal return statement
        // return true;// Uncaught SyntaxError: Illegal return statement
        // return false;// Uncaught SyntaxError: Illegal return statement
    }
    show.push(arr[i]);
}

2. Array.forEach()

var arr = ['a', 'b', 'c', 'd', 'e'];
var show = [];

arr.forEach((item, index) => {
    if (index === 2) {
        // break;// Uncaught SyntaxError: Illegal break statement
        // continue;// Uncaught SyntaxError: Illegal continue statement: no surrounding iteration statement
        // Return; // ['a','b','d','e'] can only jump out of this cycle
        // Return true; // ['a','b','d','e'] can only jump out of this cycle
        // Return false; // ['a','b','d','e'] can only jump out of this cycle
    }
    show.push(item);
})

3. for...in...

var arr = ['a', 'b', 'c', 'd', 'e'];
var show = [];

for (var item in arr) {
    if (item === '2') {
        break;// ['a','b'] Jump out of the loop successfully
        // Continue; // ['a','b','d','e'] can only jump out of this cycle
        // return;// Uncaught SyntaxError: Illegal return statement
        // return true;// Uncaught SyntaxError: Illegal return statement
        // return false;// Uncaught SyntaxError: Illegal return statement
    }

    show.push(arr[item]);
}

4. Array.map()

var arr = ['a', 'b', 'c', 'd', 'e'];
var show = [];

arr.map((item, index) => {
    if (index === 2) {
        // break;// Uncaught SyntaxError: Illegal break statement
        // continue;// Uncaught SyntaxError: Illegal continue statement: no surrounding iteration statement
        // Return; // ['a','b','d','e'] can only jump out of this cycle
        // Return true; // ['a','b','d','e'] can only jump out of this cycle
        // Return false; // ['a','b','d','e'] can only jump out of this cycle
    }
    show.push(item);
})

5. Array.some()

var arr = ['a', 'b', 'c', 'd', 'e'];
var show = [];

arr.some((item, index) => {
    if (index === 2) {
        // break;// Uncaught SyntaxError: Illegal break statement
        // continue;// Uncaught SyntaxError: Illegal continue statement: no surrounding iteration statement
        // Return; // ['a','b','d','e'] can only jump out of this cycle
        return true;// ['a','b'] successfully jumped out of the loop
        // Return false; // ['a','b','d','e'] can only jump out of this cycle
    }
    show.push(item);
})

6. Array.every()

var arr = ['a', 'b', 'c', 'd', 'e'];
var show = [];

arr.every((item, index) => {
    if (index === 2) {
        // break;// Uncaught SyntaxError: Illegal break statement
        // continue;// Uncaught SyntaxError: Illegal continue statement: no surrounding iteration statement
        // Return; // ['a','b'] successfully jumped out of the loop
        // Return true; // ['a','b','d','e'] can only jump out of this cycle
        return false;// ['a','b'] successfully jumped out of the loop
    }
    return show.push(item);
})

some() differs from every(), one true all is true in some traversal, and all true in all traversal.Returning true in some traversal exits execution, whereas everyneeds returning false to exit execution.

7. Array.filter()

var arr = ['a', 'b', 'c', 'd', 'e'];
var show = [];

arr.filter((item, index) => {
    if (index === 2) {
        // break;// Uncaught SyntaxError: Illegal break statement
        // continue;// Uncaught SyntaxError: Illegal continue statement: no surrounding iteration statement
        // Return; // ['a','b','d','e'] can only jump out of this cycle
        // Return true; // ['a','b','d','e'] can only jump out of this cycle
        return false;// ['a','b','d','e'] can only jump out of this cycle
    }
    show.push(item);
})

Posted by mojodojo on Sat, 24 Aug 2019 09:10:39 -0700