Skip to content

Instantly share code, notes, and snippets.

@JDMcKinstry
Created May 14, 2016 21:15
Show Gist options
  • Save JDMcKinstry/30a67184f95945427518b25af176b9c3 to your computer and use it in GitHub Desktop.
Save JDMcKinstry/30a67184f95945427518b25af176b9c3 to your computer and use it in GitHub Desktop.
removes duplicaates from an array. * modifies original array
/* 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