Javascript刪除數組中指定值的元素

   

Array.prototype.remove = function(index){
  if(isNaN(index) || index > this.length){return false;}
  for(var i = 0, n = 0; i < this.length; i++){
    if(this[i] != this[index]){
      this[n++] = this[i];
    }
  }
  this.length -= 1;
 }

//在數組中獲取指定值的元素索引
 Array.prototype.getIndexByValue = function(value){
  var index = -1;
  for(var i = 0; i < this.length; i++){
    if(this[i] == value){
      index = i;
      break;
    }
  }
  return index;
}數組

 //使用舉例this

arr = ["1","2","3","4","5"];spa

var index = arr.getIndexByValue("2");prototype

arr.remove(index);索引

相關文章
相關標籤/搜索