JS Quick Tips! 3 Ways to Get Unique Values from an Array. 🚀

In this super short article, learn how to create 3 different functions that return all of the unique values in an array.

This is a typical interview question and pretty handy in your day job. 🦾

You can watch the video version here or keep scrolling for the code snippets.

1) Filter the values 👇

const getUniqueValues = array => (
  array.filter((currentValue, index, arr) => arr.indexOf(currentValue) === index)
);

2) Using reduce 👇

const getUniqueValues = array => array.reduce(
  (accumulator, currentValue) => (
    accumulator.includes(currentValue) ? accumulator : [...accumulator, currentValue]
  ), []
);

3) Destructure a new Set 👇

const getUniqueValues = array => [...new Set(array)];

Follow me on Twitter

Subscribe on Codú.

JavaScriptTutorialQuick TipsWeb Developer
Avatar for Niall Maher

Written by Niall Maher

Founder of Codú - The web developer community! I've worked in nearly every corner of technology businesses; Lead Developer, Software Architect, Product Manager, CTO and now happily a Founder.

Loading

Fetching comments

Hey! 👋

Got something to say?

or to leave a comment.