Javascript Array clean(deleteValue)

Description

Javascript Array clean(deleteValue)

/**//from   ww  w . j  av a  2  s .  co  m
 * Remove all occurrences of deleteValue if array
 * @memberof module:ArrayFunctions
 * @param {object} deleteValue Value to be deleted
 * @returns {Array}
 */

Array.prototype.clean = function(deleteValue) {
  for (var i = 0; i < this.length; i++) {
    if (this[i] == deleteValue) {
      this.splice(i, 1);
      i--;
    }
  }
  return this;
};

Javascript Array clean(deleteValue)


Array.prototype.clean = function (deleteValue) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == deleteValue) {
            this.splice(i, 1);//  w  w  w .jav  a2  s .co m
            i--;
        }
    }
    return this;
};
var sub1 = new Array("", "One", "Two", "", "Three", "", "Four");
var sub2 = [ '1', '2', '3', , , '6'];
console.log(sub1);
console.log(sub1.clean());
console.log(sub1.clean(''));
console.log(sub2);
console.log(sub2.clean(''));
console.log(sub2.clean());



PreviousNext

Related