The most commonly used traversal method is for statements (also recursive and while). When we traverse an array, we usually do this:
The code copy is as follows:
var arr = [1,2,3,4,5,6,7,8,9,10];
for(var i=0,total=arr.length;i<total;i++){
console.log(i,arr[i]);
}
This is the most commonly used traversal method: positive order traversal. It goes from the first item in the array to the last item.
Then why does the small drama still mention reverse order traversal today?
Here we have to mention the most commonly used module in the component written by Xiaoju: events. Used to create custom event models, handle event listening and triggering, the simplest publish and subscribe mode. Because recently discovered that there is a hidden danger of memory overflow, it is necessary to add a method to unbind on the original basis.
Because the callback function with the same event name is placed in the same array, unbinding only requires finding the corresponding callback function from the array (the same callback function may be bound multiple times) and removing it.
It's a very simple requirement, so I naturally wrote a code similar to the following:
The code copy is as follows:
//Remove 2 in the array
var arr = [1,2,2,2,2,1,1,2,2];
for(var i=0,total=arr.length;i<total;i++){
if(arr[i] == 2){
//Meet the conditions, remove
arr.splice(i,1);
}
}
console.log(arr);
There is no normal code, but the final output is: [1, 2, 2, 1, 1, 2]. Obviously, the execution result does not meet expectations.
What's the problem?
After a careful analysis, I found that the problem is that every time the match is successful, after performing the removal operation, the next item to be checked will be skipped because each item in the array will rise one by one.
I found the problem, changed the code, and after performing the removal operation, adjust the sequence index (i).
The code copy is as follows:
//Remove 2 in the array
var arr = [1,2,2,2,2,1,1,2,2];
for(var i=0,total=arr.length;i<total;i++){
if(arr[i] == 2){
//Meet the conditions, remove
arr.splice(i,1);
//Adjust the sequence index
i = i-1;
}
}
console.log(arr);
The problem has been solved, but I always feel that modifying the sequence index is a trick to make a for loop. So a flash of inspiration, and a slap, and the following code was struck out:
The code copy is as follows:
//Remove 2 in the array
var arr = [1,2,2,2,2,1,1,2,2];
for(var i = arr.length-1;i!=-1;i--){
if(arr[i] == 2){
//Meet the conditions, remove
arr.splice(i,1);
}
}
console.log(arr);
The traversal process remains unchanged. The only thing that changes is that the order of traversal has changed. By the way, there is also one variable total missing.
OK, I admit that what I wrote today is very silky, but through this example, I remind you when writing code in the future. During the traversal process, if it involves modifying the array itself (adding and deleting), reverse traversal is a relatively safe traversal method.
Coding notes, leave them to mock yourself in the future!
Please indicate the source for reprinting: http://bh-lay.com/blog/148c07761fa