How to find distinct items in an array in JavaScript or typescript

Published on August 05, 2019

I have to sort a bunch of work items in to organisational areas so I needed to know what were the distinct categories of objects in an array. In C#/Linq there is a nice IEnumerable.Distinct(f(x)) method available but JavaScript doesn’t have something like this yet. I didn’t want to install a helper like lodash if I could avoid it. Luckily my list was simple strings so I was able to use ‘Set()‘.

// The solution is to map your objects to the array of identifiers. note: This only works if the identifier is, or can be reduced to, a primative type. If you supply objects it will use object references which might not act as you expect. There is no IEquatable in JS either.

// map objects to a list of identifier strings
let areaNames = => x.area)

//distinct categories will be distinct! The set will check if the value already exists
// A value in the Set may only occur once; it is unique in the Set's collection.
const distinctCategories = [ Set(areaNames)]

// watch out becuase undefined and NaN can be stored in the Set.
// And in the case of strings, casing is respected for uniqueness
Darragh ORiordan

Hi! I'm Darragh ORiordan.

I live and work in Sydney, Australia enjoying the mountains and the ocean.

I build and support happy teams that create high quality software for the web.

Contact me on Twitter!

Sign up for the newsletter

Get new writings, curated tech articles and coding tips!

Read the Privacy Policy.