Remove Duplicate Values from Array 2


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

This snippet is based on this snippet: http://snipplr.com/view/45323/remove-duplicate-values-from-array/
The difference is that this function is an extension of the Array object and that it cleans the duplicates from the input Array instead of creating a new array with the unique values.


Copy this code and paste it in your HTML
  1. Array.prototype.unique = function unique()
  2. {
  3. var i = 0;
  4. while (i < this.length)
  5. {
  6. for (k = this.length; k > i; k--)
  7. {
  8. if (this[k] === this[i])
  9. {
  10. this.splice(k,1);
  11. }
  12. }
  13. i++;
  14. }
  15. return this;
  16. }
  17.  
  18. var myarray = ['jeffrey', 'allie', 'patty', 'damon', 'zach', 'jeffrey', 'allie', 'patty', 'damon', 'zach', 'joe','joe','joe','adam'];
  19. alert(myarray.unique().join(', '));

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.