Javascript reverse traversal implementation code _ javascript skills

Source: Internet
Author: User
Traversal is one of the most common syntaxes in a program. In JS, small data processing, large list rendering, and ajax support for json data, more and more common traversal methods are for statements (recursive and while ). When we traverse an array, we usually do this:

The Code is as follows:


Var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
For (var I = 0, total = arr. length; I Console. log (I, arr [I]);
}

This is the most common Traversal method: Forward traversal. It goes from the first item of the array to the last one.

Why does the drama mention reverse traversal today?

Here I have to mention one of the most commonly used components in the little drama: events. It is used to create a custom event model, process event listening and triggering, and in pub/sub mode. Because of the recent memory overflow risks, you need to add a unbinding method on the original basis.

Because the callback function of the same event name is placed in the same array, you only need to find the corresponding callback function from the array (the same callback function may be bound multiple times) and remove it.

It is a simple requirement, so it is natural to write code similar to the following:

The Code is as follows:


// Remove 2 from the array
Var arr = [1, 2, 2, 2, 1, 1, 2];
For (var I = 0, total = arr. length; I If (arr [I] = 2 ){
// Meet the conditions and remove
Arr. splice (I, 1 );
}
}
Console. log (arr );

Normally, the code can be output as follows: [1, 2, 2, 1, 1, 2]. Obviously, the execution result is not as expected.

Where is the problem?

After careful analysis, it is found that the problem lies in the success of each matching. After the removal operation is executed, the next item to be checked will be skipped, because each item after the array is raised one by one.

Locate the problem, modify the code, and adjust the index (I) of the sequence index after the removal operation ).

The Code is as follows:


// Remove 2 from the array
Var arr = [1, 2, 2, 2, 1, 1, 2];
For (var I = 0, total = arr. length; I If (arr [I] = 2 ){
// Meet the conditions and remove
Arr. splice (I, 1 );
// Adjust the sequence index
I = I-1;
}
}
Console. log (arr );

The problem is solved, but I always feel that modifying the sequence index is a matter of for loop. As a result, the following code is displayed:

The Code is as follows:


// Remove 2 from the array
Var arr = [1, 2, 2, 2, 1, 1, 2];
For (var I = arr. length-1; I! =-1; I --){
If (arr [I] = 2 ){
// Meet the conditions and remove
Arr. splice (I, 1 );
}
}
Console. log (arr );

The traversal process remains unchanged. The only difference is that the order of traversal changes. By the way, a variable total is missing.

Okay, I admit that what I wrote today is quite cool. But through this example, I woke up when I wrote code later. During the traversal process, if you need to modify the array itself (add or delete), reverse traversal is an insurance Traversal method.

Coding notes for later ridicule!

Reprinted please indicate Source: http://bh-lay.com/blog/148c07761fa

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.