Remove all element from array - Node.js Array

Node.js examples for Array:Remove Element

Description

Remove all element from array

Demo Code


Array.prototype.removeAll = function( func ) {
  var _retained = [];
  var _removed = [];
  for( var i=0; i<this.length; i++ ) {
    var item = this[i];
    if( !func(item) == true ) {
      _retained.push( item );/* w ww .j a v  a  2s.  c  o m*/
    }
    else {
      _removed.push( item );
    }
  }
  this.length = 0;
  this.push.apply(this, _retained);
  return _removed;
};

Related Tutorials