Files
30-seconds-of-code/snippets/filter-unique.md
Angelos Chalaris 61200d90c4 Kebab file names
2023-04-27 21:58:35 +03:00

580 B

title, tags, cover, firstSeen, lastUpdated
title tags cover firstSeen lastUpdated
Filter unique array values array tulips-and-reeds 2020-11-02T19:41:00+02:00 2020-11-02T19:41:00+02:00

Creates an array with the unique values filtered out.

  • Use the Set constructor and the spread operator (...) to create an array of the unique values in arr.
  • Use Array.prototype.filter() to create an array containing only the non-unique values.
const filterUnique = arr =>
  [...new Set(arr)].filter(i => arr.indexOf(i) !== arr.lastIndexOf(i));
filterUnique([1, 2, 2, 3, 4, 4, 5]); // [2, 4]