TIL... a Javascript Unique Filter

2019-02-28 This post is over 2 years old

Today I was filtering a list of ‘tags’. Naturally I wanted to know each tag, but didn’t want the list to contain duplicates. So I did a quick search to find any uniqueness filters built-in to Javascript.

I found now but did find a thread on StackOverflow with a workable solution. It works by filtering for those cells who are the first occurence of their value in the array. THis works great for ‘exact’ matches, so be wary for strings where the content ‘uniqueness’ might ignore casing.

1
const onlyUnique = (value, index, self) => self.indexOf(value) === index;

Usage:

1
2
3
4
const array = [1,2,1,3,4,3,5];
const filteredArray = array.filter(onlyUnique);

// expected result = [1,2,3,4,5]