Created
November 10, 2014 16:03
-
-
Save weslleyaraujo/083d3143396a61a6e7a1 to your computer and use it in GitHub Desktop.
Unique method for Array in Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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