Posted By

ronydee on 12/12/10


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

michellebracken


Remove Duplicate Values from Array 2


 / Published in: JavaScript
 

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.

  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  

You need to login to post a comment.