Javscript删除数组中指定元素

xiaoxiao2021-02-28  89

把数组中某个值删除,并返回新数组,需要遍历旧数组找到要删除的元素

[javascript]  view plain  copy /*   * 删除数组中指定值   */   Array.prototype.remove=function(value){       var len = this.length;     for(var i=0,n=0;i<len;i++){//把出了要删除的元素赋值给新数组         if(this[i]!=value){           this[n++]=this[i];       }else{         console.log(i);//测试所用       }     }       this.length = n;   };      var arr = ['1','2','3','5','2','1','4','2','2'];   arr.remove(2);   console.log(arr);  

转载请注明原文地址: https://www.6miu.com/read-80362.html

最新回复(0)