刪除陣列中的指定元素例項程式碼

antzone發表於2017-03-21

本章節介紹一下如何刪除陣列中的指定元素,然後返回新陣列,下面就通過程式碼例項介紹一下如何實現此功能。

程式碼如下:

[JavaScript] 純文字檢視 複製程式碼
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; 
    }
  } 
  this.length=n; 
}; 
var theArray=["螞蟻部落",2,"青島市南區","antzone"]; 
theArray.remove("螞蟻部落"); 
console.log(theArray);

相關文章