Skip to content

Instantly share code, notes, and snippets.

@lukesh
Created July 18, 2013 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukesh/6033451 to your computer and use it in GitHub Desktop.
Save lukesh/6033451 to your computer and use it in GitHub Desktop.
if(!Array.prototype.unique) {
Object.defineProperty(Array.prototype, "unique", {
value: function(key) {
var o = {}, i, l = this.length, r = []
for(i=0; i<l;i+=1) {
if(key) {
var _key = ""
,self = this
if(typeof key == "string") {
_key = key
} else if(typeof key == "function") {
_key = key(self[i])
} else {
key.forEach(function(e) {
_key += self[i][e]
})
}
o[_key] = this[i]
} else {
o[this[i]] = this[i]
}
}
for(i in o) {
r.push(o[i])
}
return r
}
,writable: false
,enumerable: false
,configurable: false
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment