Javascript Array remove(val)

Description

Javascript Array remove(val)


Array.prototype.remove = function(val) {
  if(isNaN(val) || val < 0 || val > this.length) { return false; }
  return this.splice(val, 1);
}
var someArray = [1, 2, 3];
  
someArray.remove(1);/* w ww .j a  v a 2 s .c  om*/

Javascript Array remove(val)

// add remove method to Array's prototype
Array.prototype.remove = function(val) {
  // body.../*from  w  w w. j  a v a 2  s . com*/
  var idx = this.indexOf(val);

  if (idx > -1) {
    this.splice(idx, 1);
  }
};

Javascript Array remove(val)

Array.prototype.remove = function(val) {
    for(var i=0; i<this.length; i++) {
        if(this[i] == val) {
            this.splice(i, 1);//  w  w  w . j a  v  a  2s. c om
            break;
        }
    }
}

Javascript Array remove(val)

Array.prototype.remove = function(val) {
  var index = this.indexOf(val);
  if (index > -1) {
    this.splice(index, 1);/*from w w w . ja va  2s  .  c  om*/
  }
};

Javascript Array remove(val)

Array.prototype.remove = function(val) {
    var index = this.indexOf(val);
    if (index >= 0) this.splice(index, 1);
    return this;//from   w  ww. j  a va 2  s .  com
};

Javascript Array remove(val)

/*/*  w  w  w .  j  a  v  a 2 s.c o  m*/
2. Write a function that removes all elements with a given value
 * var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, "1"];
 * arr.remove(1); // arr = [2, 4, 3, 4, 111, 3, 2, "1"];

 * Attach it to the array object
 * Read about `prototype` and how to attach methods
*/

Array.prototype.remove = function(val){

    for (let i = 0; i < this.length; i++ ){
        if (this[i] === val) this.splice(i,1);
    }
    return this;
}
var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, "1"];
console.log(arr.remove(1));
// now its clear



PreviousNext

Related