Advertisement
rg443

javascript Array.remove() Method

Jan 12th, 2013
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Array.prototype.remove = function(from, to) {
  2.   var rest = this.slice((to || from) + 1 || this.length);
  3.   this.length = from < 0 ? this.length + from : from;
  4.   return this.push.apply(this, rest);
  5. };
  6.  
  7.  
  8. // usage:
  9.  
  10. // Remove the second item from the array
  11. array.remove(1);
  12. // Remove the second-to-last item from the array
  13. array.remove(-2);
  14. // Remove the second and third items from the array
  15. array.remove(1,2);
  16. // Remove the last and second-to-last items from the array
  17. array.remove(-2,-1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement