Created
March 10, 2020 14:55
-
-
Save munkacsitomi/f67acff034eb2dc4e569267f4ab23c2c to your computer and use it in GitHub Desktop.
Filter duplicated values from array
This file contains hidden or 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
const tmp = [1, 2, 3, 1, 3, 4, 5]; | |
const uniqueFirst = Array.from(new Set(tmp)); | |
const uniqueSecond = tmp.filter((item, index) => tmp.indexOf(item) === index); | |
const uniqueThird = tmp.reduce((unique, item) => unique.includes(item) ? unique : [...unique, item], []); | |
const duplicatedValues = tmp.filter((item, index) => tmp.indexOf(item) !== index); | |
console.log(uniqueFirst); | |
console.log(uniqueSecond); | |
console.log(uniqueThird); | |
console.log(duplicatedValues); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment