How JS deletes the specified content in the array

How to delete the specified content in the array?
Mainly the application of splice function, this function is very powerful and very useful.
We deal with the problem in two cases:
① Delete a specified element in the array.
There is only one element with value 1 in the array, and our task is to delete this element with value 1.

  var array = [88,1,5,6,505,85,77,50];
    for(var i = 0; i < array.length; i++){
    
    
        if(array[i] == 1){
    
    
            array.splice(i,1);
        }
    }
    console.log('删除1后数组的内容为',array);

Insert picture description here

①Delete all specified elements in the array.
There are n 1s in the array, and my task is to delete all elements with a value of 1.

var array = [88,1,1,1,5,6,1,1,1,50,1];
for(var i = 0; i < array.length; i++){
    
    
    if(array[i] == 1){
    
    
        array.splice(i,1);
        i = i-1;
    }
}
console.log('全部删除1后的数组内容为',array);

Insert picture description here

I mainly encountered these two situations. If there are other needs to solve difficulties, you can communicate more in the comment area.

There is a WeChat mini program course design, complete design needs, contact personal QQ: 505417246

Pay attention to the following WeChat public account, you can receive WeChat applet, Vue, TypeScript, front-end, uni-app, full stack, Nodejs, Python and other practical learning materials. The
latest and most complete front-end knowledge summary and project source code will be released to the WeChat public as soon as possible No., please pay attention, thank you!

Insert picture description here

Guess you like

Origin blog.csdn.net/m0_46171043/article/details/112055593