Files
30-seconds-of-code/js/s/filter-non-unique.md
Angelos Chalaris 5c913d20bd Reorganize snippets
2023-05-03 21:19:02 +03:00

594 B

title, type, language, tags, cover, dateModified
title type language tags cover dateModified
Filter non-unique array values snippet javascript
array
digital-nomad-10 2020-11-02T19:40:45+02:00

Creates an array with the non-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 unique values.
const filterNonUnique = arr =>
  [...new Set(arr)].filter(i => arr.indexOf(i) === arr.lastIndexOf(i));
filterNonUnique([1, 2, 2, 3, 4, 4, 5]); // [1, 3, 5]