[모던 자바스크립트] Set과 Map

재오·2023년 7월 31일
1

JavaScript

목록 보기
46/48
post-thumbnail

Set

Set 객체는 중복되지 않는 유일한 값들의 집합이다. Set 객체는 배열과 유사하지만 차이점도 존재한다.

  • 동일한 값을 중복하여 포함할 수 없다.
  • 요소 순서에 의미가 없다.
  • 인덱스로 요소에 접근할 수 없다.

이러한 Set 객체의 특성은 수학적 집합의 특성과 일치한다.

Set 객체의 생성

Set 객체는 Set 생성자 함수로 생성한다. Set 생성자 함수에 인수를 전달하지 않으면 빈 Set 객체가 생성된다. Set 생성자 함수는 이터러블을 인수로 전달받아 Set 객체를 생성한다. 이때 이터러블의 중복된 값은 Set 객체에 요소로 저장되지 않는다.

const set = new Set();
console.log(set); // Set(0) {}

const set1 = new Set('hello');
console.log(set1); // Set(4) {"h", "e", "l", "o"}

중복을 허용하지 않는 Set 객체의 특성을 활용하여 배열에서 중복된 요소를 제거할 수 있다.

const uniq = array => [...new Set(array)];
console.log(uniq([2, 1, 2, 3, 4, 3, 4])); // [2, 1, 3, 4]

요소 개수 확인

Set 객체의 요소 개수를 확인할 때는 Set.prototype.size 프로퍼티를 활용한다. size 프로퍼티는 setter 함수 없이 getter 함수만 존재하는 접근자 프로퍼티다. 따라서 size 프로퍼티에 숫자를 할당하여 Set 객체의 요소 개수를 변경할 수 없다.

const set = new Set([1, 2, 3]);

set.size = 10; // 무시된다
console.log(set.size); // 3

요소 추가

Set 객체에 요소를 추가할 때에는 add 메서드를 사용한다.

const set = new Set();

set.add(1);
console.log(set); // Set(1) {1}

add 메서드는 새로운 요소가 추가된 Set 객체를 반환한다. 따라서 add 메서드를 호출한 후에 add 메서드를 연속적으로 호출할 수 있다.

const set = new Set();

set.add(1).add(2);
console.log(set); // Set(2) {1, 2}

요소 존재 여부 확인

Set 객체에 특정 요소가 존재하는지 확인하려면 Set.prototype.has 메서드를 사용한다. has 메서드는 특정 요소의 존재 여부를 나타내는 불리언 값을 반환한다.

const set = new Set([1, 2, 3]);

console.log(set.has(2)); // true

요소 삭제

Set 객체의 특정 요소를 삭제하려면 Set.prototype.delete 메서드를 사용한다. delete 메서드는 삭제 성공 여부를 나타내는 불리언 값을 반환한다.

const set = new Set([1, 2, 3]);

set.delete(2);
console.log(set); // Set(2) {1, 3}

delete 메서드는 삭제 성공 여부를 나타내는 불리언 값을 반환한다. 따라서 Set.prototype.add 메서드와 달리 연속적으로 호출할 수 없다.

요소 일괄 삭제

Set 객체의 모든 요소를 일괄 삭제하려면 Set.prototype.clear 메서드를 사용한다. clear 메서드는 언제나 undefined를 반환한다.

const set = new Set([1, 2, 3]);

set.clear();
console.log(set); // set(0) {}

요소 순회

Set 객체의 요소를순회하려면 Set.prototype.forEach 메서드를 사용한다. Set.prototype.forEach 메서드는 Array.prototype.forEach 메서드와 유사하게 콜백 함수와 forEach 메서의 콜백 함수 내부에서 this로 사용될 객체를 인수로 전달한다.

  • 첫 번째 인수: 현재 순회 중인 요소값
  • 두 번째 인수: 현재 순회 중인 요소값
  • 세 번째 인수: 현재 순회 중인 Set 객체 자체

첫 번째 인수와 두 번째 인수는 같은 값이다. 이처럼 동작하는 이유는 Set 객체는 순서에 의미가 없어 배열 같이 인덱스를 갖지 않기 때문이다.

const set = new Set([1, 2, 3]);

set.forEach((v, v2, set) => console.log(v, v2, set));
/*
1 1 Set(3) { 1, 2, 3}
2 2 Set(3) { 1, 2, 3}
3 3 Set(3) { 1, 2, 3}
*/

Set 객체는 이터러블이다. 따라서 for...of 문으로 순회할 수 있으며, 스프레드 문법과 구조분해 할당의 대상이 될 수도 있다.

const set = new Set([1, 2, 3]);

for (const value of set) {
  console.log(value); // 1 2 3
}

console.log([...set]); // [1, 2, 3]

Map

Map 객체는 키와 값의 쌍으로 이루어진 컬렉션이다. Map 객체는 객체와 유사하지만 차이도 여러 존재한다. 우선 Map 객체는 객체를 포함한 모든 값을 키로 사용할 수 있다. 또한 일반 객체는 이터러블이 아니지만 Map 객체는 이터러블이다. 그리고 length가 아닌 size로 개수를 확인한다.

Map 객체의 생성

Map 객체는 Map 생성자 함수로 생성한다. Map 생성자 함수는 이터러블을 인수로 전달받아 Map 객체를 생성한다. 이때 인수로 전달되는 이터러블은 키와 값의 쌍으로 이루어진 요소로 구성되어야 한다.

const map1 = new Map([['key1', 'value1'], ['key2', 'value2']]);
console.log(map1); // Map(2) {"key1" => "value1", "key2" => "value2"}

const map2 = new Map([1, 2]); // TypeError

요소 개수 확인

Map 객체의 요소 개수를 확인할 때는 Map.prototype.size 프로퍼티를 사용한다.

const { size } = new Map([['key1', 'value1'], ['key2', 'value2']]);
console.log(size); // 2

요소 추가

Map 객체에 요소를 추가할 때는 Map.prototype.set 메서드를 사용한다.

const map = new Map();
console.log(map); // Map(0) {}

map.set('key1', 'value1');
console.log(map); // Map(1) { "key1" => "value1" }

객체는 문자열 또는 심벌 값만 키로 사용할 수 있다. 하지만 Map 객체는 키 타입에 제한이 없다. 따라서 객체를 포함한 모든 값을 키로 사용할 수 있다.

const map = new Map();

const lee = { name: 'Lee' };

map.set(lee, 'developer'); // Map(1) { {name: "Lee"} => "developer" }

요소 취득

Map 객체에서 특정 요소를 취득하려면 Map.prototype.get 메서드를 사용한다. get 메서드의 인수로 키를 전달하면 Map 객체에서 인수로 전달한 키를 갖는 값을 반환한다.

const map = new Map();

const lee = { name: 'Lee' };

map.set(lee, 'developer');

console.log(map.get(lee)); // developer

요소 삭제

Map 객체의 요소를 삭제하려면 Map.prototype.delete 메서드를 사용한다.

const map = new Map();

const lee = { name: 'Lee' };

map.delete(lee);

요소 일괄 삭제

Map 객체의 요소를 일괄 삭제하려면 Map.prototype.clear 메서드를 사용한다.

const lee = { name: 'Lee' };
const map = new Map([[lee, 'developer'], [kim, 'designer']]);

map.clear();

요소 순회

Map 객체의 요소를 순회하려면 Map.prototype.forEach 메서드를 사용한다. Map.prototype.forEach 메서드는 Array.prototype.forEach 메서드와 유사하게 콜백 함수와 forEach 메서의 콜백 함수 내부에서 this로 사용될 객체를 인수로 전달한다.

  • 첫 번째 인수: 현재 순회 중인 요소값
  • 두 번째 인수: 현재 순회 중인 요소키
  • 세 번째 인수: 현재 순회 중인 Map 객체 자체
const lee = { name: 'Lee' };
const kim = { name: 'Kim' };

const map = new Map([[lee, 'developer'], [kim, 'designer']]);

map.forEach((v, k, map) => console.log(v, k, map));

/*
developer {name: "Lee"} Map(2) {
	{name: "Lee"} => "developer",
    {name: "Kim"} => "designer"
}

developer {name: "Kim"} Map(2) {
	{name: "Lee"} => "developer",
    {name: "Kim"} => "designer"
}
*/

Map 객체는 이터러블이다. 따라서 for...of문으로 순회할 수 있으며, 스프레드 문법과 배열 구조 할당의 대상이 될 수도 있다.

const lee = { name: 'Lee' };
const kim = { name: 'Kim' };

const map = new Map([[lee, 'developer'], [kim, 'designer']]);

for(const entry of map) {
  console.log(entry); // [{name: "Lee"}, "developer"] [{name: "Kim"}, "designer"]
}

console.log([...map]); // [[{name: "Lee"}, "developer", [{name: "Kim"}, "designer"]]

const [a, b] = map;
console.log(a, b); // [{name: "Lee"}, "developer"] [{name: "Kim"}, "designer"]

Map 객체는 이터러블이면서 동시에 이터레이터인 객체를 반환하는 메서드를 제공한다.

  • Map.prototype.keys : Map 객체에서 요소키를 값으로 갖는 이터러블이면서 동시에 이터레이터인 객체를 반환한다.
  • Map.prototype.values : Map 객체에서 요소값을 값으로 갖는 이터러블이면서 동시에 이터레이터인 객체를 반환한다.
  • Map.prototype.entries : Map 객체에서 요소키와 요소값을 값으로 갖는 이터러블이면서 동시에 이터레이터인 객체를 반환한다.
const lee = { name: 'Lee' };
const kim = { name: 'Kim' };

const map = new Map([[lee, 'developer'], [kim, 'designer']]);

for(const key of map.keys()) {
  console.log(key); // {name: "Lee"} {name: "Kim"}
}

for(const entry of map.entries()) {
  console.log(entry); //  [{name: "Lee"}, "developer"] [{name: "Kim"}, "designer"]
}
profile
블로그 이전했습니다

0개의 댓글