1const names = ['John', 'Paul', 'George', 'Ringo', 'John'];
2
3let unique = [...new Set(names)];
4console.log(unique); // 'John', 'Paul', 'George', 'Ringo'
1// 1. filter()
2function removeDuplicates(array) {
3 return array.filter((a, b) => array.indexOf(a) === b)
4};
5// 2. forEach()
6function removeDuplicates(array) {
7 let x = {};
8 array.forEach(function(i) {
9 if(!x[i]) {
10 x[i] = true
11 }
12 })
13 return Object.keys(x)
14};
15// 3. Set
16function removeDuplicates(array) {
17 array.splice(0, array.length, ...(new Set(array)))
18};
19// 4. map()
20function removeDuplicates(array) {
21 let a = []
22 array.map(x =>
23 if(!a.includes(x) {
24 a.push(x)
25 })
26 return a
27};
28/THIS SITE/ "https://dev.to/mshin1995/back-to-basics-removing-duplicates-from-an-array-55he#comments"
1arr.filter((v,i,a)=>a.findIndex(t=>(t.place === v.place && t.name===v.name))===i)
1// Using the Set constructor and the spread syntax:
2
3arrayWithOnlyUniques = [...new Set(arrayWithDuplicates)]
1let myArray = ['a', 'b', 'a', 'b', 'c', 'e', 'e', 'c', 'd', 'd', 'd', 'd']
2let myOrderedArray = myArray.reduce(function (accumulator, currentValue) {
3 if (accumulator.indexOf(currentValue) === -1) {
4 accumulator.push(currentValue)
5 }
6 return accumulator
7}, [])
8
9console.log(myOrderedArray)
1let array1=[1,2,3];
2let array2=[1,2,4,5,6,7,7,4,3,2,1,4,5,5,2,3,3,1,2,9,8,6,4,3,2,7,1]
3