1const names = ['John', 'Paul', 'George', 'Ringo', 'John'];
2
3let unique = [...new Set(names)];
4console.log(unique); // 'John', 'Paul', 'George', 'Ringo'
1const names = ['John', 'Paul', 'George', 'Ringo', 'John'];
2
3let x = (names) => names.filter((v,i) => names.indexOf(v) === i)
4x(names); // 'John', 'Paul', 'George', 'Ringo'
1const names = ['John', 'Paul', 'George', 'Ringo', 'John'];
2
3function removeDups(names) {
4 let unique = {};
5 names.forEach(function(i) {
6 if(!unique[i]) {
7 unique[i] = true;
8 }
9 });
10 return Object.keys(unique);
11}
12
13removeDups(names); // // 'John', 'Paul', 'George', 'Ringo'
1let a = [10,20,30,10,30];
2let b = a.filter((item,index) => a.indexOf(item) === index);
3console.log(b);
1function toUniqueArray(a){
2 var newArr = [];
3 for (var i = 0; i < a.length; i++) {
4 if (newArr.indexOf(a[i]) === -1) {
5 newArr.push(a[i]);
6 }
7 }
8 return newArr;
9}
10var colors = ["red","red","green","green","green"];
11var colorsUnique=toUniqueArray(colors); // ["red","green"]
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}