Posted By

djmicahelb on 04/11/10


Tagged


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

r4ge
eric3884gmailcom


Javascript remove duplicates from Array


 / Published in: JavaScript
 

Use this to step through an Array and keep only values the first instance of any values, removing all subsequent duplicates.

  1. Array.prototype.myUnique = function(){
  2. var r = new Array();
  3. o:for(var i = 0; i < this.length; i++) {
  4. for(var x = 0; x < r.length; x++) {
  5. if(r[x]==this[i]) {
  6. continue o;
  7. }
  8. }
  9. r[r.length] = this[i];
  10. }
  11. return r;
  12. };
  13.  
  14. //usage
  15.  
  16. yourArray = yourArray.myUnique();

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: eric3884gmailcom on April 6, 2011

This is excellent! Shortest variation I've seen.

Posted By: eric3884gmailcom on April 6, 2011

This is excellent! Shortest variation I've seen.

Posted By: eric3884gmailcom on April 6, 2011

This is excellent! Shortest variation I've seen.

Posted By: eric3884gmailcom on April 6, 2011

This is excellent! Shortest variation I've seen.

You need to login to post a comment.