Review the native JS summary of the use of for,forEach,map, some, every, and how to break out of the loop

1. The for loop is applicable to any loop application scenario

www.runoob.com/js/js-loop-…

(The body of the while loop is similar to the body of the for loop. If the condition is met, it will jump out)

Const arr = [6]; for(let i in arr) { console.log(i); // return; Uncaught SyntaxError: Illegal return statement // break; // Break the entire loop // continue; // If the condition is met, only the current round of the loop is skipped. The next cycle continues}Copy the code

2. The forEach traversal

Suitable for convenient data, do not stop midway

www.runoob.com/jsref/jsref…

[6]. ForEach ((item, I, arr) = > {the console. The log (item); // element item console.log(I); // Array subscript console.log(arr); // return; // If this condition is met, only the current round of loop is blocked and the next round of loop continues. // Continue; // Invalid error is reported})Copy the code

3.Map traversal method

Applies to returns that accept a new array

www.runoob.com/jsref/jsref…

Let arr = [1,2,3,4,5,6].map(()=>{return true; }); console.log(arr); //[true, true, true, true, true, true]Copy the code

4. Every method

This is used to retrieve whether “all” items in an array satisfy a condition and return a Boolean value.

www.runoob.com/jsref/jsref…

Const arr = [6]; Let resBool = arr.every((item)=>{// return false; False return item>0; // return true if each item is greater than 0; // Return true if each item is greater than 0; // Return false if each item is greater than 0; console.log(resBool); // trueCopy the code

5. Some methods

Applies to retrieving whether an item in an array satisfies a condition and returns a Boolean value.

www.runoob.com/jsref/jsref…

Const arr = [6]; let bool = arr.some((item) =>{ // return item >8; False return item >5; // return true => return true; // Return false; // Return false; Otherwise, the default is return false}); console.log(bool); // trueCopy the code