Summary of ten methods of traversal in JavaScript
- 2021-10-13 06:38:48
- OfStack
1. while cycle
while is followed by a loop condition and an execution statement. As long as the condition is met, the execution inside will be executed directly
var i = 0
while(i<10){
console.log(i)
i++
}
2. do... while loop
It is similar to while, but it will execute it once before making the judgment condition
var i=0
do{
console,log(i)
i++
}while(i<10)
3. for cycle
var arr = [0,1,2,3]
for(let i = 0;i < arr.length;i++){
console.log(i)
}
4. forEach cycle
var arr = [0,1,2,3]
arr.forEach((item,index)=>{
console.log(` Subscript ${index} The number of is ${item}`)
})
5. for... in loop
1 is used to traverse the attributes of objects, and if you traverse arrays, you will get subscripts
var obj = {name:'snail',age:18}
for(var key in obj){
console.log(key)
}
6. for... of loop
You can traverse every 1 element
var str = 'snail'
for(var item of str){
console.log(item)
}
7. map mapping
The map method passes all the members of the array into the parameter function in turn, and then returns the results of each execution into a new array.
When this function is called, the map method passes it three parameters: the current member, the current position, and the array itself.
Note: 1 new array is returned without changing the original array.
var arr = [0,1,2,3]
arr.map((item)=>{
return item*2
})
8. filter () Filter Cycle
The filter method is used to filter array members, and the members that meet the conditions form a new array to return.
Its argument is a function, which is executed by all array members in turn, and the members whose return result is true form a new array to return. This method does not change the original array.
var arr = [0,4,2,3,5]
arr.filter((item)=>{
return (item>3)
})
9. some (), every () traversal
These two methods return a Boolean value to determine whether an array member meets a condition.
They take 1 function as an argument, and all array members execute this function in turn. The function takes three arguments: the current member, the current position, and the entire array, and returns a Boolean value.
The some method is that the return value of the entire some method is true as long as the return value of one member is true, otherwise false is returned.
The every method, on the other hand, returns true only if all members return true, otherwise false is returned. Comparing the two, some () returns true as long as one is true; every () returns false as long as one of them is false.
These two methods are very useful in actual development. For example, the two methods can be used to traverse the circular array when judging whether the user checks inoperable data or whether one operable data is checked.
10. reduce () traversal
The first argument of the reduce method is a function. The function takes the following four arguments.
1. Accumulator (accumulator)
2. Current Value (current value)
3. Current Index (current index)
4. Source Array (Source Array)
The return value of the function is assigned to the accumulator, which is remembered in each iteration of the array and finally becomes the final single result value.
Of these four parameters, only the first two are required and the last two are optional.