/ 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.
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.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
Array.prototype.unique = function unique() { var i = 0; while (i < this.length) { for (k = this.length; k > i; k--) { if (this[k] === this[i]) { this.splice(k,1); } } i++; } return this; } var myarray = ['jeffrey', 'allie', 'patty', 'damon', 'zach', 'jeffrey', 'allie', 'patty', 'damon', 'zach', 'joe','joe','joe','adam']; alert(myarray.unique().join(', '));