Skip to content

Instantly share code, notes, and snippets.

@lizlongnc
Created November 12, 2014 00:56
Show Gist options
  • Save lizlongnc/bbee40c794f12a624a4a to your computer and use it in GitHub Desktop.
Save lizlongnc/bbee40c794f12a624a4a to your computer and use it in GitHub Desktop.
Filter out duplicates from an array
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