filter, map, some, find, every array of methods out of the loop

Introduction
native array of methods -MDN

Array many ways, sometimes we need out of the loop in order to improve the efficiency of the code.

This article is pasted someone else article (end text has Portal), the following methods are tested, such as test results have different welcome message

Conclusion summary

1 for circulation success out of this cycle illegal illegal illegal √2 Array.forEach () illegal illegal jump out of this cycle out of this cycle out of this cycle × 3 for ... in this cycle is not successful jump legal illegal illegal √4 Array.map () is not valid in the current cycle is not legitimate bounce bounce out of the current cycle of this cycle × 5 Array.some () is not valid in the current cycle is not a legitimate successful jump out of this cycle √6 Array.every () does not legal in the current cycle out legitimate successful successful √7 Array.filter () is not valid in the current cycle is not legitimate bounce bounce out of the current cycle of this cycle ×

forEach, map and filter currently I do not know any way to stop the current traversal, tables (pictures) can be mentioned are out of this cycle,

- 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'] 成功跳出循环
        // continue;// ['a', 'b', 'd', 'e'] 只能跳出本次循环
        // return;// Uncaught SyntaxError: Illegal return statement
        // return true;// Uncaught SyntaxError: Illegal return statement
        // return false;// Uncaught SyntaxError: Illegal return statement
    }
    show.push(arr[i]);
}
  • 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"] 只能跳出本次循环
        // return true;// ["a", "b", "d", "e"] 只能跳出本次循环
        // return false;// ['a', 'b', 'd', 'e'] 只能跳出本次循环
    }
    show.push(item);
})

for…in…

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

for (var item in arr) {
    if (item === '2') {
        break;// ["a", "b"] 跳出循环成功
        // continue;// ["a", "b", "d", "e"] 只能跳出本次循环
        // return;// Uncaught SyntaxError: Illegal return statement
        // return true;// Uncaught SyntaxError: Illegal return statement
        // return false;// Uncaught SyntaxError: Illegal return statement
    }

    show.push(arr[item]);
}

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"] 只能跳出本次循环
        // return true;// ["a", "b", "d", "e"] 只能跳出本次循环
        // return false;// ["a", "b", "d", "e"] 只能跳出本次循环
    }
    show.push(item);
})
  • 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"] 只能跳出本次循环
        return true;// ["a", "b"] 成功跳出循环
        // return false;// ["a", "b", "d", "e"] 只能跳出本次循环
    }
    show.push(item);
})
  • 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"] 成功跳出循环
        // return true;// ["a", "b", "d", "e"] 只能跳出本次循环
        return false;// ["a", "b"] 成功跳出循环
    }
    return show.push(item);
})

some with every return values ​​are Boolean values, some () and every () different, some traversal is true of all that is true, but every traversal all true job. some traversal returns true will withdraw, and every you need to return false will be carried out.

  • 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"] 只能跳出本次循环
        // return true;// ["a", "b", "d", "e"] 只能跳出本次循环
        return false;// ["a", "b", "d", "e"] 只能跳出本次循环
    }
    show.push(item);
})

original

Published 15 original articles · won praise 3 · Views 3438

Guess you like

Origin blog.csdn.net/qq_39370934/article/details/102870909