This is my second article on getting started
A subset of methods in an array
One of the most common things in a for array is a for loop
- Define a local variable in a loop statement. When the loop ends, the local variable is released.
- Break: End the entire cycle in the circulatory body
- Continue: Finish the loop and proceed directly to the next loop
forEach
- The downside of being more concise: you can’t use a break statement to break a loop, and you can’t use a return statement to return to an outer function
- The disadvantage is that each element must be traversed
const arr=[1.2.3.4.5];
arr.forEach((item) = > {
console.log(item) //2 //3 //4 //5
});
Copy the code
every
- A false is a false, and as long as one of the elements is false, the element after it is not traversed. The next traversal is determined by returning true or false, and the effects of break and continue can be written
for of
The for of loop is used to get the value in a key-value pair, while the for in gets the key name
- A data structure that deploys the Symbol. Iterator property is considered to have an iterator interface and can use the for of loop.
- There is no Symbol. Iterator attribute, so using for of will say obj is not iterable
- Unlike forEach, for of can be used with break, continue, and return, meaning that a for of loop can exit the loop at any time.
for in
- for … The in loop returns values that are the key names of the data structures.
Iterating over the key value of the object returned by the object, iterating over the key of the array returned by the array.
- for … The in loop iterates not only over numeric key names, but also over values on the prototype and other keys added manually.
How to create an array
- let arr = [];
- let arr1 = Array(5)
Think about the problem that you want to create an array of length 5 with values of 1
Array.from({length:5},function(){return 1})
Array(5).fill(1)
Let arr = Array(5) add a value to it with a forEach loop, but a for loop does