Skip to content

Instantly share code, notes, and snippets.

@weslleyaraujo
Created November 10, 2014 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weslleyaraujo/083d3143396a61a6e7a1 to your computer and use it in GitHub Desktop.
Save weslleyaraujo/083d3143396a61a6e7a1 to your computer and use it in GitHub Desktop.
Unique method for Array in Javascript
Array.prototype.unique = function () {
var cached = [];
return this.filter(function (index) {
if (cached.indexOf(index) === -1) {
cached.push(index);
return index;
}
}.bind(this));
};
console.log([1, 2, 3, 4, 4, 'a', 'a', -1, -1].unique());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment