Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save YakovSPb/ed7aec0a456d20156dfa6f3674cd6943 to your computer and use it in GitHub Desktop.
Save YakovSPb/ed7aec0a456d20156dfa6f3674cd6943 to your computer and use it in GitHub Desktop.
Как удалить дубликаты из массива
Как удалить дубликаты из массива
=================
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