Last active
October 10, 2020 12:14
-
-
Save YakovSPb/ed7aec0a456d20156dfa6f3674cd6943 to your computer and use it in GitHub Desktop.
Как удалить дубликаты из массива
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
Как удалить дубликаты из массива | |
================= | |
1. Set | |
const array = ['Alex', 1, 'mark', 1, 'Mellisa', 1]; | |
const uneqArray = [... new Set(array)]; | |
const uneqArray2 = Array.from(new Set(array)); | |
2. indexOf(находит первый элемент) | |
const array = ['Alex', 1, 'mark', 1, 'Mellisa', 1]; | |
const uneqArray = array.filter((item, index) => index === array.indexOf(item)); | |
3. reduce | |
const array = ['Alex', 1, 'mark', 1, 'Mellisa', 1]; | |
const uneqArray = array.reduce((uniq, item) => { | |
return uniq.includes(item) ? uniq : [...uniq, item]; | |
}, []); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment