Created
June 23, 2011 14:30
-
-
Save abozhilov/1042640 to your computer and use it in GitHub Desktop.
Array.prototype.unique
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 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