Javascript Array remove(obj)

Description

Javascript Array remove(obj)


Array.prototype.remove = function(obj) {
  for(var i=0; i<this.length; i++) {
    if(this[i] === obj) {
      this.splice(i, 1);//  w ww .  ja  va 2 s.  co  m
      return true;
    };
  };
  return false;
};

Javascript Array remove(obj)

Array.prototype.remove = function(obj) {
    for (var i = 0, j = 0; j < this.length; ++i, ++j) {
        if (this[j] === obj)
            ++j;/*from w ww  .j a va2s  .c  o m*/
        if (i != j) {
            this[i] = this[j];
            this[j] = undefined;
        }
    }
    while (this.length > 0 && this[this.length - 1] === undefined)
        this.pop();
};

Javascript Array remove(obj)

/**//from  www .j  a va  2s. c o m
 * Removes an object from the array.
 * @param {Object} obj - The object to remove.
 * @returns {Object} The removed object, or undefined if no object was found to be removed.
 */
Array.prototype.remove = function(obj) {
  var index = this.indexOf(obj);
  if(index !== -1) {
    return this.splice(index, 1)[0];
  }
  return undefined;
}

Javascript Array remove(obj)

Array.prototype.remove = function(obj){
        var flag = false;
        for(var i=0;i<this.length;i++){
            if(this[i] == obj){
                flag = !flag;/*from   ww w . j  a  va2 s.c om*/
                break;
            }
        }
        if(flag)this.splice(i,1);
}

Javascript Array remove(obj)

// javascript file

Array.prototype.remove = function(obj) { 
    for (var i = 0; i < this.length; i++){ 
        var temp = this[i];
        // isNaN("") return false
        if (obj != "" && !isNaN(obj)) {
            temp = i; // w ww .j a va 2 s .  c  o m
        } 
        if (temp == obj) { 
            for (var j = i; j <this.length; j++) { 
                this[j] = this[j+1]; 
            } 
            this.length = this.length-1; 
        } 
    }
}

Javascript Array remove(obj)

Array.prototype.remove = function(obj){
 var i=0,n=0;/*from   www .j a  va 2  s.c  o  m*/
 for(i=0;i<this.length;i++){
  if(this[i] != obj){
   this[n++] = this[i];
  }
 }
 if(n<i){
  this.length = n;
 }
};

Javascript Array remove(obj)

Array.prototype.remove = function(obj){
 return this.splice(this.indexOf(obj), 1);
}

Javascript Array remove(obj)

/**/*from  w  w w  . j  a  va 2  s .c o m*/
 * Removes an object from the array. Uses indexOf to find the object in the array.
 * @param obj The object to be removed.
 */
Array.prototype.remove = function (obj) {
    var index = this.indexOf(obj);
    if(index !== -1) {
        this.splice(index, 1);
    }
};

Javascript Array remove(obj)

Array.prototype.remove = function(obj){
 var l = this.length;
 var replace = false;
 for(var i=0; i<l; i++){
  if(this[i] == obj){
   replace = true;//from  w ww . j a  va2 s  .co m
  }
  if(replace && i<l){
   this[i] = this[i+1];
  }
 }
 if(replace){
  this.pop();
 }
};



PreviousNext

Related