Skip to content

Instantly share code, notes, and snippets.

@joates
Created May 17, 2016 22:55
Show Gist options
  • Save joates/1a03072904552f0b1499fc6db896f7b2 to your computer and use it in GitHub Desktop.
Save joates/1a03072904552f0b1499fc6db896f7b2 to your computer and use it in GitHub Desktop.
extract *only* unique values from an existing array
// private implementation of '[].unique'
Array.prototype.unique = function() {
var orig = {}
, i
, il = this.length
, res = []
for (i = 0; i < il; i++) orig[this[i]] = this[i]
for (i in orig) res.push(orig[i])
return res
}
@joates
Copy link
Author

joates commented May 17, 2016

usage:

include this snippet in your code and use it like this...
var array_unique = new Array().unique.call(original_array)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment