Created
November 12, 2014 00:56
-
-
Save lizlongnc/bbee40c794f12a624a4a to your computer and use it in GitHub Desktop.
Filter out duplicates from an array
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
from https://coderwall.com/p/nilaba/simple-pure-javascript-array-unique-method-with-5-lines-of-code | |
Array.prototype.unique = function() { | |
return this.filter(function (value, index, self) { | |
return self.indexOf(value) === index; | |
}); | |
} | |
var arr = ['a', 1, 'a', 2, '1'] | |
arr.unique(); // => ['a', 1, 2, '1'] | |
This solution does NOT require any dependencies like jQuery or prototype.js, and it works for arrays with mixed types! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment