배열 디스트럭처링 할당의 대상은 이터러블이어야 하고, 할당 기준은 베열의 인덱스
set객체는 중복되지 않는 유일한 값들의 집합
중복을 허용하지 않는 특성을 활용하여 배열에서 중복된 요소 제거 가능
// 배열의 중복 요소 제거
const uniq = array => array.filter((v, i, self) => self.indexOf(v) === i);
console.log(uniq([2, 1, 2, 3, 4, 3, 4])); // [2, 1, 3, 4]
// Set을 사용한 배열의 중복 요소 제거
const uniq = array => [...new Set(array)];
console.log(uniq([2, 1, 2, 3, 4, 3, 4])); // [2, 1, 3, 4]
요소 개수 확인 시 Set.prototype.size프로퍼티 사용
const set = new Set();
set
.add(1)
.add('a')
.add(true)
.add(undefined)
.add(null)
.add({})
.add([]);
console.log(set); // Set(7) {1, "a", true, undefined, null, {}, []}
const set = new Set([1, 2, 3]);
// 요소 2를 삭제한다.
set.delete(2);
console.log(set); // Set(2) {1, 3}
// 요소 1을 삭제한다.
set.delete(1);
console.log(set); // Set(1) {3}
교집합, 합집합, 차집합 들을 구현 가능
키와 값의 쌍으로 이루어진 컬렉션
Map.prototype.size 프로퍼티를 사용
const map = new Map([['key1', 'value1'], ['key2', 'value2']]);
console.log(Object.getOwnPropertyDescriptor(Map.prototype, 'size'));
// {set: undefined, enumerable: false, configurable: true, get: ƒ}
map.size = 10; // 무시된다.
console.log(map.size); // 2
Map.prototype.forEach메서드를 사용
3개의 인수를 전달받음