Menu

gitpiper

filterNonUnique javascript Code Snippet in 2023

arraybeginner

Last Updated: 4 June 2023

Creates an array with the non-unique values filtered out.

  • Use new Set() 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]

javascript snippet similar to filterNonUnique For You in June 2023

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2023 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️