Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all unique values in a JavaScript array (remove duplicates)

I have an array of numbers that I need to make sure are unique. I found the code snippet below on the internet and it works great until the array has a zero in it. I found this other script here on Stack Overflow that looks almost exactly like it, but it doesn't fail.

So for the sake of helping me learn, can someone help me determine where the prototype script is going wrong?

Array.prototype.getUnique = function() {  var o = {}, a = [], i, e;  for (i = 0; e = this[i]; i++) {o[e] = 1};  for (e in o) {a.push (e)};  return a; } 

More answers from duplicate question:

  • Remove duplicate values from JS array

Similar question:

  • Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array
like image 917
Mottie Avatar asked Dec 25 '09 04:12

Mottie


People also ask

How do you remove duplicates from an array of arrays?

To remove duplicates from an array: First, convert an array of duplicates to a Set . The new Set will implicitly remove duplicate elements. Then, convert the set back to an array.

Does spread operator remove duplicates?

Use the spread operator to remove duplicates items and to sort a array of numbers and objects, without destruct the original array.


2 Answers

With JavaScript 1.6 / ECMAScript 5 you can use the native filter method of an Array in the following way to get an array with unique values:

function onlyUnique(value, index, self) {   return self.indexOf(value) === index; }  // usage example: var a = ['a', 1, 'a', 2, '1']; var unique = a.filter(onlyUnique);  console.log(unique); // ['a', 1, 2, '1']

The native method filter will loop through the array and leave only those entries that pass the given callback function onlyUnique.

onlyUnique checks, if the given value is the first occurring. If not, it must be a duplicate and will not be copied.

This solution works without any extra library like jQuery or prototype.js.

It works for arrays with mixed value types too.

For old Browsers (<ie9), that do not support the native methods filter and indexOf you can find work arounds in the MDN documentation for filter and indexOf.

If you want to keep the last occurrence of a value, simply replace indexOf with lastIndexOf.

With ES6 this can be shorten to:

// usage example: var myArray = ['a', 1, 'a', 2, '1']; var unique = myArray.filter((v, i, a) => a.indexOf(v) === i);  console.log(unique); // unique is ['a', 1, 2, '1']

Thanks to Camilo Martin for hint in comment.

ES6 has a native object Set to store unique values. To get an array with unique values you could now do this:

var myArray = ['a', 1, 'a', 2, '1'];  let unique = [...new Set(myArray)];  console.log(unique); // unique is ['a', 1, 2, '1']

The constructor of Set takes an iterable object, like an Array, and the spread operator ... transform the set back into an Array. Thanks to Lukas Liese for hint in comment.

like image 118
TLindig Avatar answered Oct 08 '22 14:10

TLindig


Updated answer for ES6/ES2015: Using the Set and the spread operator (thanks le-m), the single line solution is:

let uniqueItems = [...new Set(items)] 

Which returns

[4, 5, 6, 3, 2, 23, 1] 
like image 28
A.T. Avatar answered Oct 08 '22 12:10

A.T.