Ensure all elements in array are unique


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

Ensures all elements in an array are unique

newArray = myArray.unique();


Copy this code and paste it in your HTML
  1. Array.prototype.unique = function() {
  2. var newArray=new Array();
  3. label:for (var i=0; i < this.length; i++) {
  4. for (var j=0; j < newArray.length; j++) {
  5. if (newArray[j] == this[i])
  6. continue label;
  7. }
  8. newArray[newArray.length] = this[i];
  9. }
  10. return newArray;
  11. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.