1const names = ['John', 'Paul', 'George', 'Ringo', 'John'];
2
3let unique = [...new Set(names)];
4console.log(unique); // 'John', 'Paul', 'George', 'Ringo'
1let arr = [1,2,3,1,1,1,4,5]
2
3let filtered = arr.filter((item,index) => arr.indexOf(item) === index)
4
5 console.log(filtered) // [1,2,3,4,5]
1const numbers = [1 , 21, 21, 34 ,12 ,34 ,12];
2const removeRepeatNumbers = array => [... new Set(array)]
3removeRepeatNumbers(numbers) // [ 1, 21, 34, 12 ]
1uniqueArray = a.filter(function(item, pos) {
2 return a.indexOf(item) == pos;
3})
4
1//ES6
2let uniqueArray = [...new Set(arrayWithDuplicates)];
3
4//Alternative
5function removeArrayDuplicates(arrayWithDuplicates) {
6 let seen = {};
7 let uniqueArray = [];
8 let len = arrayWithDuplicates.length;
9 let j = 0;
10 for(let i = 0; i < len; i++) {
11 let item = arrayWithDuplicates[i];
12 if(seen[item] !== 1) {
13 seen[item] = 1;
14 uniqueArray[j++] = item;
15 }
16 }
17 return uniqueArray;
18}