Ask questions and get good answers on jQuery

I have to remove an item from an array. Is there a method in jQuery to handle same a function remove?

I managed to do so only by extending the Array prototype, but in jQuery?

Array.prototype.remove = function(s) {
  var i = this.indexOf(s);
  if(i != -1) this.splice(i, 1);
}

asked Nov 10 '09 at 10:17

Federico%20Soldani's gravatar image

Federico Soldani
173128

edited Nov 10 '09 at 20:46

Pietro%20Polsinelli's gravatar image

Pietro Polsinelli ♦♦
407


John Resig has a great solution:

// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
};
This answer is marked "community wiki".

answered Apr 06 '11 at 16:55

harking's gravatar image

harking
11

I solved in this way:

var arr = new Array();
arr = jQuery.grep(arr, function(value) {
    return value != removeItem;
});

See documentation

answered Nov 10 '09 at 14:59

Federico%20Soldani's gravatar image

Federico Soldani
173128

edited Nov 13 '09 at 19:10

Your answer
toggle preview

jQuery main site - Matteo Bicocchi