js algorithm to remove elements in the array

Insert picture description here

function removeWithoutCopy(arr, item) {
    
    
    for(var i=0;i<arr.length;i++)
        {
    
    
            if(arr[i]==item)
                {
    
    
                    arr.splice(i,1);
                    i--;
                }
        }
    return arr;
}

The core idea: loop traversal, if it is equal to item, delete itself. At the same time i–, i++ will run to the previous position in the for loop, because the previous array will move one place to the left as a whole.

Guess you like

Origin blog.csdn.net/qq_37805832/article/details/115153369