Array Remove Function


/ Published in: JavaScript
Save to your folder(s)

remove(array) --> array

Array.remove(a) simply removes all the items in the passed in array from the current scope array. Good for bulk operations.


Copy this code and paste it in your HTML
  1. /**
  2.  * Remove all passed in items from the current array. If they
  3.  * dont exist they are ignored.
  4.  */
  5. Array.prototype.remove = function(a){
  6.  
  7. // Array Remove - By John Resig (MIT Licensed)
  8. var r = function(from, to) {
  9. if(from != -1){
  10. var rest = this.slice((to || from) + 1 || this.length);
  11. this.length = from < 0 ? this.length + from : from;
  12. return this.push.apply(this, rest);
  13. }
  14. };
  15.  
  16. for(var i=0; i<a.length;i++){
  17. r.call(this, this.indexOf(a[i]))
  18. }
  19.  
  20. return this;
  21. }
  22.  
  23. /** non prototype extending method ---------------------------- */
  24.  
  25. Array.remove = function(t, a){
  26. // Array Remove - By John Resig (MIT Licensed)
  27. var r = function(from, to) {
  28. if(from != -1){
  29. var rest = this.slice((to || from) + 1 || this.length);
  30. this.length = from < 0 ? this.length + from : from;
  31. return this.push.apply(this, rest);
  32. }
  33. };
  34.  
  35. for(var i=0; i<a.length;i++){
  36. r.call(t, t.indexOf(a[i]))
  37. }
  38.  
  39. return t;
  40. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.