javascript remove elements from an array of loop

JavaScript applications in the development process, often encounter remove the specified element in the cycle needs.

According to the conventional thinking is that the array is a for loop, then determine if the loop inside, you can delete the specified elements in judgment.

But the reality is often not run as smoothly as expected.

The problem of reduction scenarios

( Function () {
     var ARR = [. 1, 2, 2,. 3,. 4,. 5 ];
     for ( var I = 0; I <arr.length; I ++ ) {
         // Print Fact array, facilitate tracking of the array data changes 
        the console.log (I + '=' + ARR [I]);
         // delete all of the elements 2 
        IF (ARR [I] === 2 ) { 
            arr.splice (I, . 1 ); 
        } 
    } 
    the console.log (ARR); 
}) ();

You can see from the final result, this code actually removes only one of the elements out of the match, while the other elements of the matching conditions still exist, does not meet the program was originally designed.

The process is run from the print out is not difficult to find the cause of this result is because, when the delete an element, the array index of the element (subscript) occurred changes in real time, resulting in abnormal program.

way of solving the problem

Find the cause of the problem, it is not difficult to solve the problem.

Method 1: real-time adjustment index array elements corresponding relationship.

( Function () {
     var ARR = [. 1, 2, 2,. 3,. 4,. 5 ];
     for ( var I = 0; I <arr.length; I ++ ) {
         // Print Fact array, facilitate tracking of the array data changes 
        the console.log (I + '=' + ARR [I]);
         // delete all of the elements 2 
        IF (ARR [I] === 2 ) { 
            arr.splice (I -,. 1 ) ; 
        } 
    } 
    the console.log (ARR); 
}) ();

The problem occurs because, because after removing an element out of the middle, index of the element behind the element (subscript) are forward plus one, then we can manually reset cycle index after removing elements now as to the corresponding index (i--).

Method 2: from the back through the array elements.

( Function () {
     var ARR = [. 1, 2, 2,. 3,. 4,. 5 ];
     for ( var I = arr.length -. 1; I> = 0; i-- ) {
         // Print array where , to facilitate the tracking data in the array changes 
        the console.log (I + '=' + ARR [I]);
         // delete all of the elements 2 
        IF (ARR [I] === 2 ) { 
            arr.splice (I , . 1 ); 
        } 
    } 
    the console.log (ARR); 
}) ();

The problem occurs because, because after removing an element out of the middle, index of the element behind the element (subscript) are forward plus 1, then as long as the forward loop from the back, you can not control the elements behind index (subscript) a.

Method 3: Use for loop instead of a while loop (loop or forward from the rear).

(function () {
    var arr = [1, 2, 2, 3, 4, 5];
    var i = arr.length;
    while(i--) {
        console.log(i + ' = ' + arr[i]);
        if(arr[i] === 2) {
            arr.splice(i, 1);
        }
     }
    console.log(arr);
})();

Consistent with the above principles from the back loop, just use a for loop while loop instead.

 

"Today is tomorrow's yesterday, yesterday is a new day."

Guess you like

Origin www.cnblogs.com/yanggb/p/11596430.html