πŸš€ Quick tips! 3 ways to get unique values from an array.

πŸš€ Quick tips! 3 ways to get unique values from an array.

Β·

1 min read

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

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ΓΊ Community