Set() 메서드를 쓰면 문자열이나 배열에서 문자를 하나씩만 남기고 제거할 수 있다.
const my_string = "My name is Carver";
console.log([...new Set(my_string)]);
// ['M', 'y', ' ', 'n', 'a', 'm', 'e', 'i', 's', 'C', 'r', 'v']
has() 메서드는 Set에 특정 문자가 포함되어 있는지 확인한다.
const set1 = new Set([1, 2, 3, 4, 5]);
console.log(set1.has(1));
// Expected output: true
console.log(set1.has(5));
// Expected output: true
console.log(set1.has(6));
// Expected output: false
add() 메서드로 중복 문자를 추가하면 아무런 변화도 일어나지 않는다.
const mySet = new Set();
mySet.add(1); // Set [ 1 ]
mySet.add(5); // Set [ 1, 5 ]
mySet.add(5); // Set [ 1, 5 ]
mySet.add("some text"); // Set [ 1, 5, 'some text' ]
const o = { a: 1, b: 2 };
mySet.add(o);
console.log(mySet); // Set(4) { 1, 5, 'some text', { a: 1, b: 2 } }