Last active
March 14, 2018 09:11
-
-
Save chekit/c6581bd7948405211377628fd8b1b02a to your computer and use it in GitHub Desktop.
reduces digits array from repeated items to unique items only
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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