Skip to content

Instantly share code, notes, and snippets.

@avishwakarma
Last active June 28, 2016 06:36
Show Gist options
  • Save avishwakarma/2f8a929d0314349f6005227603833598 to your computer and use it in GitHub Desktop.
Save avishwakarma/2f8a929d0314349f6005227603833598 to your computer and use it in GitHub Desktop.
JavaScript Array function for unique elements
/**
* unique - Array function for unique elements
* @use [2, 2, 3, 4, 3, 2].unqiue() // [2, 3, 4]
*/
Array.prototype.unique = function() {
var unique = [];
for (var i = 0; i < this.length; i++) {
if (unique.indexOf(this[i]) == -1) {
unique.push(this[i]);
}
}
return unique;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment