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

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

Javscript删除数组中指定元素

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-06-19
  • 2022-01-13
  • 2021-07-05
  • 2021-07-27
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-01
  • 2021-06-16
  • 2021-10-09
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案