Unique Array Values

By  on  

When you look at any programming language, you see missing features that you find puzzling because the use case seems so common.  One such case is retrieving unique values from an array with JavaScript. Years ago I mentioned an easy way of unique value management using objects instead of arrays, but that's not always an option and doesn't match every use case.

Want to retrieve a unique array of values from an array that may include duplicate values?  You can use new JavaScript spread operator with Set to get an array of unique values:

var j = [...new Set([1, 2, 3, 3])]
>> [1, 2, 3]

Getting unique array values is another awesome usage of the spread operator.  And don't forget you can merge object properties with the spread operator!

There's no better feeling than being able to remove a library to complete a task that should be native to the language.  This trick brings us one step closer to that!

Recent Features

  • By
    9 More Mind-Blowing WebGL Demos

    With Firefox OS, asm.js, and the push for browser performance improvements, canvas and WebGL technologies are opening a world of possibilities.  I featured 9 Mind-Blowing Canvas Demos and then took it up a level with 9 Mind-Blowing WebGL Demos, but I want to outdo...

  • By
    I’m an Impostor

    This is the hardest thing I've ever had to write, much less admit to myself.  I've written resignation letters from jobs I've loved, I've ended relationships, I've failed at a host of tasks, and let myself down in my life.  All of those feelings were very...

Incredible Demos

  • By
    Image Reflection with jQuery and MooTools

    One subtle detail that can make a big difference on any web design is the use of image reflections. Using them too often can become obnoxious but using reflections on large, "masthead" images is a classy enhancement. Unfortunately creating image reflections within your...

  • By
    CSS Fixed Position Background Image

    Backgrounds have become an integral part of creating a web 2.0-esque website since gradients have become all the rage. If you think gradient backgrounds are too cliche, maybe a fixed position background would work for you? It does provide a neat inherent effect by...

Discussion

  1. Patrick Denny

    If you’re forced to stay in EC5-land without the spread operator, you can use the pollyfillable Array.from()

    var unique = Array.from(new Set([1,2,2,3,3,3,4,5])); // [1,2,3,4,5]
  2. Rob Harris

    Doesn’t seem to work for arrays of objects unless I’m missing something.

    • Muhammad Al Faris

      If your array is an object, you can use map function, to get the value in an array.

      then you can run method like above.

  3. Anybody using babel with this one should be cautious, this will result in an array with a single set element. Patrick Denny’s is the most predictable form.

  4. Thanks David! Very helpful! :)

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!