1const names = ['John', 'Paul', 'George', 'Ringo', 'John'];
2
3let unique = [...new Set(names)];
4console.log(unique); // 'John', 'Paul', 'George', 'Ringo'
1var myArr = [1, 2, 2, 2, 3];
2var mySet = new Set(myArr);
3myArr = [...mySet];
4console.log(myArr);
5// 1, 2, 3
1// for TypeScript and JavaScript
2const initialArray = ['a', 'a', 'b',]
3finalArray = Array.from(new Set(initialArray)); // a, 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"]
1let chars = ['A', 'B', 'A', 'C', 'B'];
2let uniqueChars = [...new Set(chars)];
3
4console.log(uniqueChars);
5Code language: JavaScript (javascript)