Skip to content

Instantly share code, notes, and snippets.

@chekit
Last active March 14, 2018 09:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chekit/c6581bd7948405211377628fd8b1b02a to your computer and use it in GitHub Desktop.
Save chekit/c6581bd7948405211377628fd8b1b02a to your computer and use it in GitHub Desktop.
reduces digits array from repeated items to unique items only
// Reduces digits array from repeated items to unique items only
const arr = [1, 2, 1, 1, 4, 5, 7, 8, 2];
const unique = arr.filter((item, index, arr) => index === arr.indexOf(item));
console.log(unique); // [1, 2, 4, 5, 7, 8]
// Alternative
const unique2 = Array.from(arr.reduce((acc, item) => acc.add(item), new Set()));
console.log(unique2); // [1, 2, 4, 5, 7, 8]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment