Created
May 14, 2016 21:15
-
-
Save JDMcKinstry/30a67184f95945427518b25af176b9c3 to your computer and use it in GitHub Desktop.
removes duplicaates from an array. * modifies original array
This file contains hidden or 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() { // removes duplicaates from an array. * modifies original array | |
var name = "unique"; | |
function unique() { | |
var x = []; | |
for (var c = {}, d = [], a = 0; a < this.length; a++) { | |
var b = this[a].constructor.name, b = b + (/num|str|regex|bool/i.test(b) ? this[a] : JSON.stringify(this[a])); | |
if (b in c) x.push(a); | |
b in c || d.push(this[a]); | |
c[b] = 1; | |
} | |
if (x) for (var i = (x.length-1); i >= 0; i--) this.splice(x[i], 1); | |
return this; | |
} | |
Object['defineProperty'] && !Array.prototype.hasOwnProperty(name) | |
? Object.defineProperty(Array.prototype, name, { value: unique }) : Array.prototype[name] = unique; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment