Skip to content

Instantly share code, notes, and snippets.

@abozhilov
Created June 23, 2011 14:30
Show Gist options
  • Save abozhilov/1042640 to your computer and use it in GitHub Desktop.
Save abozhilov/1042640 to your computer and use it in GitHub Desktop.
Array.prototype.unique
Array.prototype.unique = (function () {
var TYPE_MAP = {
'object' : 1,
'function' : 1
}
return function () {
var map = {},
objects = [],
unique = [];
for (var i = 0, len = this.length; i < len; i++) {
var val = this[i];
if (TYPE_MAP[typeof val] && val) {
if (!val.__unique__) {
unique.push(val);
objects.push(val);
val.__unique__ = 1;
}
}
else if (!map[val]) {
unique.push(val);
map[val] = 1;
}
}
for (i = objects.length; i--;) {
delete objects[i].__unique__;
}
return unique;
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment