Javascript remove duplicates from Array


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

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


Copy this code and paste it in your HTML
  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


You need to login to view comments.