ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

Set

Set ๊ฐ์ฒด๋Š” ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” ์œ ์ผํ•œ ๊ฐ’๋“ค์˜ ์ง‘ํ•ฉ์œผ๋กœ, ๋ฐฐ์—ด๊ณผ ์œ ์‚ฌํ•˜์ง€๋งŒ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฐจ์ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๊ตฌ๋ถ„ ๋ฐฐ์—ด Set ๊ฐ์ฒด
๋™์ผํ•œ ๊ฐ’์„ ์ค‘๋ณตํ•˜์—ฌ ํฌํ•จํ•  ์ˆ˜ ์žˆ๋‹ค. O X
์š”์†Œ ์ˆœ์„œ์— ์˜๋ฏธ๊ฐ€ ์žˆ๋‹ค. O X
์ธ๋ฑ์Šค๋กœ ์š”์†Œ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. O X

 

์ด์ฒ˜๋Ÿผ Set ๊ฐ์ฒด์˜ ํŠน์„ฑ์€ ์ˆ˜ํ•™์  ์ง‘ํ•ฉ์˜ ํŠน์ •๊ณผ ์ผ์น˜ํ•˜๋ฉฐ Set์€ ์ˆ˜ํ•™์  ์ง‘ํ•ฉ์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ์ž๋ฃŒ๊ตฌ์กฐ์ž„์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ Set ์„ ํ†ตํ•ด ๊ต์ง‘ํ•ฉ, ํ•ฉ์ง‘ํ•ฉ, ์ฐจ์ง‘ํ•ฉ, ์—ฌ์ง‘ํ•ฉ ๋“ฑ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

Set ๊ฐ์ฒด์˜ ์ƒ์„ฑ

Set ๊ฐ์ฒด๋Š” Set ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋กœ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

  • Set ์ƒ์„ฑ์ž ํ•จ์ˆ˜์— ์ธ์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜์ง€ ์•Š์œผ๋ฉด ๋นˆ Set ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ
  • Set ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋Š” ์ดํ„ฐ๋Ÿฌ๋ธ”์„ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์•„ Set ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•จ
  • ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์€ ์ดํ„ฐ๋Ÿฌ๋ธ”์˜ ์ค‘๋ณต๋œ ๊ฐ’์€ Set ๊ฐ์ฒด ์š”์†Œ๋กœ ์ €์žฅ๋˜์ง€ ์•Š์Œ
// 1. ์ธ์ˆ˜ ์ „๋‹ฌ X
const set = new Set();
console.log(set); // Set(0) {}

// 2. ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์€ ์ดํ„ฐ๋Ÿฌ๋ธ”์˜ ์ค‘๋ณต๋œ ๊ฐ’์€ ๋ฌด์‹œ๋จ
const set1 = new Set([1, 2, 3, 3]);
console.log(set1); // Set(3) {1, 2, 3}

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

 

๋”ฐ๋ผ์„œ ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š” 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 ๊ฐ์ฒด์˜ ์š”์†Œ ๊ฐœ์ˆ˜๋ฅผ ํ™•์ธํ•  ๋•Œ๋Š” Set.prototype.size ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

 

const { size } = new Set([1, 2, 3, 3]);
console.log(size); // 3

 

size ํ”„๋กœํผํ‹ฐ๋Š” setter ํ•จ์ˆ˜ ์—†์ด getter ํ•จ์ˆ˜๋งŒ ์กด์žฌํ•˜๋Š” ์ ‘๊ทผ์ž ํ”„๋กœํผํ‹ฐ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ size ํ”„๋กœํผํ‹ฐ์— ์ˆซ์ž๋ฅผ ํ• ๋‹นํ•˜์—ฌ Set ๊ฐ์ฒด์˜ ์š”์†Œ ๊ฐœ์ˆ˜๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

 

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

console.log(Object.getOwnPropertyDescriptor(Set.prototype, 'size'));
// {set: undefined, enumerable: false, configurable: true, get: ƒ}

set.size = 10; // ๋ฌด์‹œ๋œ๋‹ค.
console.log(set.size); // 3

 

์š”์†Œ ์ถ”๊ฐ€

Set ๊ฐ์ฒด์— ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•  ๋•Œ๋Š” Set.prototype.add ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

 

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

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

 

Set ๊ฐ์ฒด์™€ add ๋ฉ”์„œ๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํŠน์ง•์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

  • add ๋ฉ”์„œ๋“œ๋Š” ์ƒˆ๋กœ์šด ์š”์†Œ๊ฐ€ ์ถ”๊ฐ€๋œ Set ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— add ๋ฉ”์„œ๋“œ๋ฅผ ์—ฐ์†์ ์„ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์Œ
  • Set ๊ฐ์ฒด์— ์ค‘๋ณต๋œ ์š”์†Œ๋Š” add ์—ฐ์‚ฐ์„ ํ•ด๋„ ๋ฌด์‹œ๋จ (์—๋Ÿฌ ๋ฐœ์ƒX)
  • Set ๊ฐ์ฒด๋Š” NaN๊ณผ NaN, +0๊ณผ -0์„ ๊ฐ™๋‹ค๊ณ  ํ‰๊ฐ€ํ•˜์—ฌ ์ค‘๋ณต ์ถ”๊ฐ€๋ฅผ ํ—ˆ์šฉํ•˜์ง€ ์•Š์Œ
  • Set ๊ฐ์ฒด๋Š” ๊ฐ์ฒด๋‚˜ ๋ฐฐ์—ด๊ณผ ๊ฐ™์ด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ชจ๋“  ๊ฐ’์„ ์š”์†Œ๋กœ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Œ
// 1. ์—ฐ์†์  ํ˜ธ์ถœ
const set = new Set();

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

// 2. ์ค‘๋ณต๊ฐ’ ๋ฌด์‹œ
const set = new Set();

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

// 3. NaN๊ณผ +0,-0
const set = new Set();

console.log(NaN === NaN); // false
console.log(0 === -0); // true

// NaN๊ณผ NaN์„ ๊ฐ™๋‹ค๊ณ  ํ‰๊ฐ€ํ•˜์—ฌ ์ค‘๋ณต ์ถ”๊ฐ€๋ฅผ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.
set.add(NaN).add(NaN);
console.log(set); // Set(1) {NaN}

// +0๊ณผ -0์„ ๊ฐ™๋‹ค๊ณ  ํ‰๊ฐ€ํ•˜์—ฌ ์ค‘๋ณต ์ถ”๊ฐ€๋ฅผ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.
set.add(0).add(-0);
console.log(set); // Set(2) {NaN, 0}

// 4. add ๊ฐ€๋Šฅํ•œ ์š”์†Œ๋“ค
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, {}, []}

 

์š”์†Œ ์กด์žฌ ์—ฌ๋ถ€ ํ™•์ธ

Set ๊ฐ์ฒด์— ํŠน์ • ์š”์†Œ๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•˜๋ ค๋ฉด Set.prototype.has ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ, has ๋ฉ”์„œ๋“œ๋Š” ํŠน์ • ์š”์†Œ์˜ ์กด์žฌ ์—ฌ๋ถ€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ถˆ๋ฆฌ์–ธ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

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

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

 

์š”์†Œ ์‚ญ์ œ

Set ๊ฐ์ฒด์— ํŠน์ • ์š”์†Œ๋ฅผ ์‚ญ์ œํ•˜๋ ค๋ฉด Set.prototype.delete ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ, delete ๋ฉ”์„œ๋“œ๋Š” ํŠน์ • ์š”์†Œ์˜ ์‚ญ์ œ ์„ฑ๊ณต ์—ฌ๋ถ€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ถˆ๋ฆฌ์–ธ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

// 1. ์‚ญ์ œํ•˜๋ ค๋Š” ์š”์†Œ๊ฐ’์„ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ
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}

// 2. ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์š”์†Œ๋ฅผ ์‚ญ์ œ
const set = new Set([1, 2, 3]);

// ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์š”์†Œ 0์„ ์‚ญ์ œํ•˜๋ฉด ์—๋Ÿฌ์—†์ด ๋ฌด์‹œ๋œ๋‹ค.
set.delete(0);
console.log(set); // Set(3) {1, 2, 3}

// 3. add์™€ ๋‹ฌ๋ฆฌ ์—ฐ์†์  ํ˜ธ์ถœ ๋ถˆ๊ฐ€๋Šฅ
const set = new Set([1, 2, 3]);

// delete๋Š” ๋ถˆ๋ฆฌ์–ธ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
set.delete(1).delete(2); // TypeError: set.delete(...).delete is not a function
  • ์ธ๋ฑ์Šค๊ฐ€ ์•„๋‹ˆ๋ผ ์‚ญ์ œํ•˜๋ ค๋Š” ์š”์†Œ๊ฐ’์„ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•ด์•ผ ํ•จ (Set ๊ฐ์ฒด๋Š” ์ˆœ์„œ์— ์˜๋ฏธ๊ฐ€ ์—†์–ด ์ธ๋ฑ์Šค๊ฐ€ ์—†์Œ)
  • ์กด์žฌํ•˜์ง€ ์•Š๋Š” Set ๊ฐ์ฒด์˜ ์š”์†Œ๋ฅผ ์‚ญ์ œํ•˜๋ ค ํ•˜๋ฉด ์—๋Ÿฌ ์—†์ด ๋ฌด์‹œ๋จ
  • delete ๋ฉ”์„œ๋“œ๋Š” ๋ถˆ๋ฆฌ์–ธ ๋ฐ˜ํ™˜๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์—ฐ์†์ ์œผ๋กœ ํ˜ธ์ถœ์ด ๋ถˆ๊ฐ€๋Šฅ

์š”์†Œ ์ผ๊ด„ ์‚ญ์ œ

Set ๊ฐ์ฒด์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์ผ๊ด„ ์‚ญ์ œํ•˜๋ ค๋ฉด Set.prototype.clear ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ, clear ๋ฉ”์„œ๋“œ๋Š” ์–ธ์ œ๋‚˜ undefined๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

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

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

 

์š”์†Œ ์ˆœํšŒ

Set ๊ฐ์ฒด๋ฅผ ์ˆœํšŒํ•˜๋ ค๋ฉด Set.prototype.forEach ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ, forEach ๋ฉ”์„œ๋“œ๋Š” ์ฝœ๋ฐฑ ํ•จ์ˆ˜์™€ forEach ๋ฉ”์„œ๋“œ์˜ ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ this๋กœ ์‚ฌ์šฉ๋  ๊ฐ์ฒด ์˜ต์…˜์„ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

  • ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜ : ํ˜„์žฌ ์ˆœํšŒ ์ค‘์ธ ์š”์†Œ๊ฐ’
  • ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜ : ํ˜„์žฌ ์ˆœํšŒ ์ค‘์ธ ์š”์†Œ๊ฐ’
  • ์„ธ ๋ฒˆ์งธ ์ธ์ˆ˜ : ํ˜„์žฌ ์ˆœํšŒ ์ค‘์ธ Set ๊ฐ์ฒด ์ž์ฒด

์ด์ฒ˜๋Ÿผ ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜์™€ ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜๊ฐ€ ๊ฐ™์€ ๊ฐ’์ธ ์ด์œ ๋Š” Array.prototype.forEach ๋ฉ”์„œ๋“œ์™€ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ํ†ต์ผํ•˜๊ธฐ ์œ„ํ•จ์ด๋ฉฐ ๋‹ค๋ฅธ ์˜๋ฏธ๋Š” ์—†์Šต๋‹ˆ๋‹ค. 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]);

// Set ๊ฐ์ฒด๋Š” Set.prototype์˜ Symbol.iterator ๋ฉ”์„œ๋“œ๋ฅผ ์ƒ์†๋ฐ›๋Š” ์ดํ„ฐ๋Ÿฌ๋ธ”์ด๋‹ค.
console.log(Symbol.iterator in set); // true

// ์ดํ„ฐ๋Ÿฌ๋ธ”์ธ Set ๊ฐ์ฒด๋Š” for...of ๋ฌธ์œผ๋กœ ์ˆœํšŒํ•  ์ˆ˜ ์žˆ๋‹ค.
for (const value of set) {
  console.log(value); // 1 2 3
}

// ์ดํ„ฐ๋Ÿฌ๋ธ”์ธ Set ๊ฐ์ฒด๋Š” ์Šคํ”„๋ ˆ๋“œ ๋ฌธ๋ฒ•์˜ ๋Œ€์ƒ์ด ๋  ์ˆ˜ ์žˆ๋‹ค.
console.log([...set]); // [1, 2, 3]

// ์ดํ„ฐ๋Ÿฌ๋ธ”์ธ Set ๊ฐ์ฒด๋Š” ๋ฐฐ์—ด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น์˜ ๋Œ€์ƒ์ด ๋  ์ˆ˜ ์žˆ๋‹ค.
const [a, ...rest] = [...set];
console.log(a, rest); // 1, [2, 3]

 

์ด์ฒ˜๋Ÿผ Set ๊ฐ์ฒด๋Š” ์š”์†Œ์˜ ์ˆœ์„œ์— ์˜๋ฏธ๋ฅผ ๊ฐ–์ง€ ์•Š์ง€๋งŒ, ์ดํ„ฐ๋Ÿฌ๋ธ”์˜ ์ˆœํšŒ์™€ ํ˜ธํ™˜์„ฑ์„ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์ˆœํšŒ ์ˆœ์„œ๋Š” ์š”์†Œ๊ฐ€ ์ถ”๊ฐ€๋œ ์ˆœ์„œ๋ฅผ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค.

 

์ง‘ํ•ฉ ์—ฐ์‚ฐ

Set ๊ฐ์ฒด๋Š” ์ˆ˜ํ•™์  ์ง‘ํ•ฉ์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ์ž๋ฃŒ๊ตฌ์กฐ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ Set ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด ๊ต์ง‘ํ•ฉ, ํ•ฉ์ง‘ํ•ฉ, ์ฐจ์ง‘ํ•ฉ ๋“ฑ์˜ ์ง‘ํ•ฉ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ํ”„๋กœํ† ํƒ€์ž… ๋ฉ”์„œ๋“œ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๊ต์ง‘ํ•ฉ

๊ต์ง‘ํ•ฉ A∩B๋Š” ์ง‘ํ•ฉ A์™€ ์ง‘ํ•ฉ B์˜ ๊ณตํ†ต ์š”์†Œ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค.

 

// 1. ๊ตฌํ˜„๋ฐฉ๋ฒ• 1 
Set.prototype.intersection = function (set) {
  const result = new Set();

  for (const value of set) {
    // 2๊ฐœ์˜ set์˜ ์š”์†Œ๊ฐ€ ๊ณตํ†ต๋˜๋Š” ์š”์†Œ์ด๋ฉด ๊ต์ง‘ํ•ฉ์˜ ๋Œ€์ƒ์ด๋‹ค.
    if (this.has(value)) result.add(value);
  }

  return result;
};

const setA = new Set([1, 2, 3, 4]);
const setB = new Set([2, 4]);

// setA์™€ setB์˜ ๊ต์ง‘ํ•ฉ
console.log(setA.intersection(setB)); // Set(2) {2, 4}
// setB์™€ setA์˜ ๊ต์ง‘ํ•ฉ
console.log(setB.intersection(setA)); // Set(2) {2, 4}


// 2. ๊ตฌํ˜„๋ฐฉ๋ฒ• 2
Set.prototype.intersection = function (set) {
  return new Set([...this].filter(v => set.has(v)));
};

const setA = new Set([1, 2, 3, 4]);
const setB = new Set([2, 4]);

// setA์™€ setB์˜ ๊ต์ง‘ํ•ฉ
console.log(setA.intersection(setB)); // Set(2) {2, 4}
// setB์™€ setA์˜ ๊ต์ง‘ํ•ฉ
console.log(setB.intersection(setA)); // Set(2) {2, 4}

 

ํ•ฉ์ง‘ํ•ฉ

ํ•ฉ์ง‘ํ•ฉ A∪B๋Š” ์ง‘ํ•ฉ A์™€ ์ง‘ํ•ฉ B์˜ ์ค‘๋ณต ์—†๋Š” ๋ชจ๋“  ์š”์†Œ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค.

 

// 1. ๊ตฌํ˜„๋ฐฉ๋ฒ• 1 
Set.prototype.union = function (set) {
  // this(Set ๊ฐ์ฒด)๋ฅผ ๋ณต์‚ฌ
  const result = new Set(this);

  for (const value of set) {
    // ํ•ฉ์ง‘ํ•ฉ์€ 2๊ฐœ์˜ Set ๊ฐ์ฒด์˜ ๋ชจ๋“  ์š”์†Œ๋กœ ๊ตฌ์„ฑ๋œ ์ง‘ํ•ฉ์ด๋‹ค. ์ค‘๋ณต๋œ ์š”์†Œ๋Š” ํฌํ•จ๋˜์ง€ ์•Š๋Š”๋‹ค.
    result.add(value);
  }

  return result;
};

const setA = new Set([1, 2, 3, 4]);
const setB = new Set([2, 4]);

// setA์™€ setB์˜ ํ•ฉ์ง‘ํ•ฉ
console.log(setA.union(setB)); // Set(4) {1, 2, 3, 4}
// setB์™€ setA์˜ ํ•ฉ์ง‘ํ•ฉ
console.log(setB.union(setA)); // Set(4) {2, 4, 1, 3}


// 2. ๊ตฌํ˜„๋ฐฉ๋ฒ• 2
Set.prototype.union = function (set) {
  return new Set([...this, ...set]);
};

const setA = new Set([1, 2, 3, 4]);
const setB = new Set([2, 4]);

// setA์™€ setB์˜ ํ•ฉ์ง‘ํ•ฉ
console.log(setA.union(setB)); // Set(4) {1, 2, 3, 4}
// setB์™€ setA์˜ ํ•ฉ์ง‘ํ•ฉ
console.log(setB.union(setA)); // Set(4) {2, 4, 1, 3}

 

์ฐจ์ง‘ํ•ฉ

์ฐจ์ง‘ํ•ฉ A-B๋Š” ์ง‘ํ•ฉ A์—๋Š” ์กด์žฌํ•˜์ง€๋งŒ ์ง‘ํ•ฉ B์—๋Š” ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์š”์†Œ๋กœ ๊ตฌ์„ฑ๋ฉ๋‹ˆ๋‹ค.

 

// 1. ๊ตฌํ˜„๋ฐฉ๋ฒ• 1 
Set.prototype.difference = function (set) {
  // this(Set ๊ฐ์ฒด)๋ฅผ ๋ณต์‚ฌ
  const result = new Set(this);

  for (const value of set) {
    // ์ฐจ์ง‘ํ•ฉ์€ ์–ด๋Š ํ•œ์ชฝ ์ง‘ํ•ฉ์—๋Š” ์กด์žฌํ•˜์ง€๋งŒ ๋‹ค๋ฅธ ํ•œ์ชฝ ์ง‘ํ•ฉ์—๋Š” ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์š”์†Œ๋กœ ๊ตฌ์„ฑ๋œ ์ง‘ํ•ฉ์ด๋‹ค.
    result.delete(value);
  }

  return result;
};

const setA = new Set([1, 2, 3, 4]);
const setB = new Set([2, 4]);

// setA์— ๋Œ€ํ•œ setB์˜ ์ฐจ์ง‘ํ•ฉ
console.log(setA.difference(setB)); // Set(2) {1, 3}
// setB์— ๋Œ€ํ•œ setA์˜ ์ฐจ์ง‘ํ•ฉ
console.log(setB.difference(setA)); // Set(0) {}


// 2. ๊ตฌํ˜„๋ฐฉ๋ฒ• 2
Set.prototype.difference = function (set) {
  return new Set([...this].filter(v => !set.has(v)));
};

const setA = new Set([1, 2, 3, 4]);
const setB = new Set([2, 4]);

// setA์— ๋Œ€ํ•œ setB์˜ ์ฐจ์ง‘ํ•ฉ
console.log(setA.difference(setB)); // Set(2) {1, 3}
// setB์— ๋Œ€ํ•œ setA์˜ ์ฐจ์ง‘ํ•ฉ
console.log(setB.difference(setA)); // Set(0) {}

 

๋ถ€๋ถ„ ์ง‘ํ•ฉ๊ณผ ์ƒ์œ„ ์ง‘ํ•ฉ

์ง‘ํ•ฉ A๊ฐ€ ์ง‘ํ•ฉ B์— ํฌํ•จ๋˜๋Š” ๊ฒฝ์šฐ(A⊆B) ์ง‘ํ•ฉ A๋Š” ์ง‘ํ•ฉ B์˜ ๋ถ€๋ถ„ ์ง‘ํ•ฉ์ด๋ฉฐ, ์ง‘ํ•ฉ B๋Š” ์ง‘ํ•ฉ A์˜ ์ƒ์œ„ ์ง‘ํ•ฉ์ž…๋‹ˆ๋‹ค.

 

// 1. ๊ตฌํ˜„ ๋ฐฉ๋ฒ• 1
// this๊ฐ€ subset์˜ ์ƒ์œ„ ์ง‘ํ•ฉ์ธ์ง€ ํ™•์ธํ•œ๋‹ค.
Set.prototype.isSuperset = function (subset) {
  for (const value of subset) {
    // superset์˜ ๋ชจ๋“  ์š”์†Œ๊ฐ€ subset์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ํฌํ•จํ•˜๋Š”์ง€ ํ™•์ธ
    if (!this.has(value)) return false;
  }

  return true;
};

const setA = new Set([1, 2, 3, 4]);
const setB = new Set([2, 4]);

// setA๊ฐ€ setB์˜ ์ƒ์œ„ ์ง‘ํ•ฉ์ธ์ง€ ํ™•์ธํ•œ๋‹ค.
console.log(setA.isSuperset(setB)); // true
// setB๊ฐ€ setA์˜ ์ƒ์œ„ ์ง‘ํ•ฉ์ธ์ง€ ํ™•์ธํ•œ๋‹ค.
console.log(setB.isSuperset(setA)); // false

// 2. ๊ตฌํ˜„ ๋ฐฉ๋ฒ• 2
// this๊ฐ€ subset์˜ ์ƒ์œ„ ์ง‘ํ•ฉ์ธ์ง€ ํ™•์ธํ•œ๋‹ค.
Set.prototype.isSuperset = function (subset) {
  const supersetArr = [...this];
  return [...subset].every(v => supersetArr.includes(v));
};

const setA = new Set([1, 2, 3, 4]);
const setB = new Set([2, 4]);

// setA๊ฐ€ setB์˜ ์ƒ์œ„ ์ง‘ํ•ฉ์ธ์ง€ ํ™•์ธํ•œ๋‹ค.
console.log(setA.isSuperset(setB)); // true
// setB๊ฐ€ setA์˜ ์ƒ์œ„ ์ง‘ํ•ฉ์ธ์ง€ ํ™•์ธํ•œ๋‹ค.
console.log(setB.isSuperset(setA)); // false

Map

Map ๊ฐ์ฒด๋Š” ํ‚ค์™€ ๊ฐ’์˜ ์Œ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ์ปฌ๋ ‰์…˜์œผ๋กœ, ๊ฐ์ฒด์™€ ์œ ์‚ฌํ•˜์ง€๋งŒ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฐจ์ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๊ตฌ๋ถ„ ๊ฐ์ฒด Map ๊ฐ์ฒด
ํ‚ค๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ’ ๋ฌธ์ž์—ด ๋˜๋Š” ์‹ฌ๋ฒŒ ๊ฐ’ ๊ฐ์ฒด๋ฅผ ํฌํ•จํ•œ ๋ชจ๋“  ๊ฐ’
์ดํ„ฐ๋Ÿฌ๋ธ” X O
์š”์†Œ ๊ฐœ์ˆ˜ ํ™•์ธ Object.keys(obj).length map.size

 

Map ๊ฐ์ฒด์˜ ์ƒ์„ฑ

Map ๊ฐ์ฒด๋Š” Map ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋กœ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

  • Map ์ƒ์„ฑ์ž ํ•จ์ˆ˜์— ์ธ์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜์ง€ ์•Š์œผ๋ฉด ๋นˆ Map ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ
  • Map ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋Š” ํ‚ค์™€ ๊ฐ’์˜ ์Œ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ์š”์†Œ๋กœ ๊ตฌ์„ฑ๋œ ์ดํ„ฐ๋Ÿฌ๋ธ”์„ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์•„ Map ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•จ
  • ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์€ ์ดํ„ฐ๋Ÿฌ๋ธ”์— ์ค‘๋ณต๋œ ํ‚ค๋ฅผ ๊ฐ–๋Š” ์š”์†Œ๊ฐ€ ์กด์žฌํ•˜๋ฉด ๊ฐ’์„ ๋ฎ์–ด์”€ (์ค‘๋ณต๋œ ํ‚ค๋ฅผ ๊ฐ–๋Š” ์š”์†Œ X)
// 1. ์ธ์ˆ˜ X
const map = new Map();
console.log(map); // Map(0) {}

// 2. key์™€ value๋ฅผ ์ธ์ˆ˜๋กœ ๊ฐ–๋Š” ์ดํ„ฐ๋Ÿฌ๋ธ”์„ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ
const map1 = new Map([['key1', 'value1'], ['key2', 'value2']]);
console.log(map1); // Map(2) {"key1" => "value1", "key2" => "value2"}

const map2 = new Map([1, 2]); // TypeError: Iterator value 1 is not an entry object

// 3. ์ค‘๋ณต X
const map = new Map([['key1', 'value1'], ['key1', 'value2']]);
console.log(map); // Map(1) {"key1" => "value2"}

 

์š”์†Œ ๊ฐœ์ˆ˜ ํ™•์ธ

Map ๊ฐ์ฒด์˜ ์š”์†Œ ๊ฐœ์ˆ˜๋ฅผ ํ™•์ธํ•  ๋•Œ๋Š” Map.prototype.size ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

 

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

 

size ํ”„๋กœํผํ‹ฐ๋Š” setter ํ•จ์ˆ˜ ์—†์ด getter ํ•จ์ˆ˜๋งŒ ์กด์žฌํ•˜๋Š” ์ ‘๊ทผ์ž ํ”„๋กœํผํ‹ฐ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ size ํ”„๋กœํผํ‹ฐ์— ์ˆซ์ž๋ฅผ ํ• ๋‹นํ•˜์—ฌ Map ๊ฐ์ฒด์˜ ์š”์†Œ ๊ฐœ์ˆ˜๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

 

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 ๊ฐ์ฒด์— ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•  ๋•Œ๋Š” Map.prototype.set ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

 

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

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

 

Map ๊ฐ์ฒด์™€ set ๋ฉ”์„œ๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํŠน์ง•์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

  • set ๋ฉ”์„œ๋“œ๋Š” ์ƒˆ๋กœ์šด ์š”์†Œ๊ฐ€ ์ถ”๊ฐ€๋œ Map ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์— set ๋ฉ”์„œ๋“œ๋ฅผ ์—ฐ์†์ ์„ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ์Œ
  • Map ๊ฐ์ฒด์˜ ์ค‘๋ณต๋œ ํ‚ค๋ฅผ ๊ฐ–๋Š” ์š”์†Œ๋ฅผ setํ•˜๋ฉด ๊ฐ’์ด ๋ฎ์–ด์จ์ง (์—๋Ÿฌ ๋ฐœ์ƒX)
  • Map ๊ฐ์ฒด๋Š” NaN๊ณผ NaN, +0๊ณผ -0์„ ๊ฐ™๋‹ค๊ณ  ํ‰๊ฐ€ํ•˜์—ฌ ์ค‘๋ณต ์ถ”๊ฐ€๋ฅผ ํ—ˆ์šฉํ•˜์ง€ ์•Š์Œ
  • Map ๊ฐ์ฒด๋Š” ๋ฌธ์ž์—ด ๋˜๋Š” ์‹ฌ๋ฒŒ ๊ฐ’๋งŒ ํ‚ค๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ์ฒด์™€ ๋‹ค๋ฅด๊ฒŒ ๊ฐ์ฒด๋ฅผ ํฌํ•จํ•œ ๋ชจ๋“  ๊ฐ’์„ ํ‚ค๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ
// 1. ์—ฐ์†์  ํ˜ธ์ถœ
const map = new Map();

map
  .set('key1', 'value1')
  .set('key2', 'value2');

console.log(map); // Map(2) {"key1" => "value1", "key2" => "value2"}

// 2. ์ค‘๋ณตํ‚ค ๋ฎ์–ด์”€
const map = new Map();

map
  .set('key1', 'value1')
  .set('key1', 'value2');

console.log(map); // Map(1) {"key1" => "value2"}

// 3. NaN๊ณผ +0,-0
const map = new Map();

console.log(NaN === NaN); // false
console.log(0 === -0); // true

// NaN๊ณผ NaN์„ ๊ฐ™๋‹ค๊ณ  ํ‰๊ฐ€ํ•˜์—ฌ ์ค‘๋ณต ์ถ”๊ฐ€๋ฅผ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.
map.set(NaN, 'value1').set(NaN, 'value2');
console.log(map); // Map(1) { NaN => 'value2' }

// +0๊ณผ -0์„ ๊ฐ™๋‹ค๊ณ  ํ‰๊ฐ€ํ•˜์—ฌ ์ค‘๋ณต ์ถ”๊ฐ€๋ฅผ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.
map.set(0, 'value1').set(-0, 'value2');
console.log(map); // Map(2) { NaN => 'value2', 0 => 'value2' }

// 4. ๊ฐ์ฒด๋„ ํ‚ค๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
const map = new Map();

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

// ๊ฐ์ฒด๋„ ํ‚ค๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
map
  .set(lee, 'developer')
  .set(kim, 'designer');

console.log(map);
// Map(2) { {name: "Lee"} => "developer", {name: "Kim"} => "designer" }

 

์š”์†Œ ์ทจ๋“

Map ๊ฐ์ฒด์—์„œ ํŠน์ • ์š”์†Œ๋ฅผ ์ทจ๋“ํ•˜๋ ค๋ฉด Map.prototype.get ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

 

get ๋ฉ”์„œ๋“œ์˜ ์ธ์ˆ˜๋กœ ํ‚ค๋ฅผ ์ „๋‹ฌํ•˜๋ฉด Map ๊ฐ์ฒด์—์„œ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•œ ํ‚ค๋ฅผ ๊ฐ–๋Š” ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , ์ „๋‹ฌํ•œ ํ‚ค๋ฅผ ๊ฐ–๋Š” ์š”์†Œ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด undefined๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

const map = new Map();

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

map
  .set(lee, 'developer')
  .set(kim, 'designer');

console.log(map.get(lee)); // developer
console.log(map.get('key')); // undefined

 

์š”์†Œ ์กด์žฌ ์—ฌ๋ถ€ ํ™•์ธ

Map ๊ฐ์ฒด์— ํŠน์ • ์š”์†Œ๊ฐ€ ์กด์žฌํ•˜์ง€๋Š” ํ™•์ธํ•˜๋ ค๋ฉด Map.prototype.has ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ, has ๋ฉ”์„œ๋“œ๋Š” ํŠน์ • ์š”์†Œ์˜ ์กด์žฌ ์—ฌ๋ถ€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ถˆ๋ฆฌ์–ธ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

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

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

console.log(map.has(lee)); // true
console.log(map.has('key')); // false

 

์š”์†Œ ์‚ญ์ œ

Map ๊ฐ์ฒด์— ํŠน์ • ์š”์†Œ๋ฅผ ์‚ญ์ œํ•˜๋ ค๋ฉด Map.prototype.delete ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ, delete ๋ฉ”์„œ๋“œ๋Š” ํŠน์ • ์š”์†Œ์˜ ์‚ญ์ œ ์„ฑ๊ณต ์—ฌ๋ถ€๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ถˆ๋ฆฌ์–ธ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

// 1. ์‚ญ์ œํ•˜๋ ค๋Š” ์š”์†Œ์˜ ํ‚ค๋ฅผ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ
const lee = { name: 'Lee' };
const kim = { name: 'Kim' };

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

map.delete(kim);
console.log(map); // Map(1) { {name: "Lee"} => "developer" }

// 2. ์กด์žฌํ•˜์ง€ ์•Š๋Š” ์š”์†Œ๋ฅผ ์‚ญ์ œ
const map = new Map([['key1', 'value1']]);

// ์กด์žฌํ•˜์ง€ ์•Š๋Š” ํ‚ค 'key2'๋กœ ์š”์†Œ๋ฅผ ์‚ญ์ œํ•˜๋ ค ํ•˜๋ฉด ์—๋Ÿฌ์—†์ด ๋ฌด์‹œ๋œ๋‹ค.
map.delete('key2');
console.log(map); // Map(1) {"key1" => "value1"}

// 3. set๊ณผ ๋‹ฌ๋ฆฌ ์—ฐ์†์  ํ˜ธ์ถœ ๋ถˆ๊ฐ€๋Šฅ
const lee = { name: 'Lee' };
const kim = { name: 'Kim' };

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

map.delete(lee).delete(kim); // TypeError: map.delete(...).delete is not a function
  • ์‚ญ์ œํ•˜๋ ค๋Š” ์š”์†Œ์˜ ํ‚ค๋ฅผ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•ด์•ผ ํ•จ
  • ์กด์žฌํ•˜์ง€ ์•Š๋Š” Map ๊ฐ์ฒด์˜ ์š”์†Œ๋ฅผ ์‚ญ์ œํ•˜๋ ค ํ•˜๋ฉด ์—๋Ÿฌ ์—†์ด ๋ฌด์‹œ๋จ
  • delete ๋ฉ”์„œ๋“œ๋Š” ๋ถˆ๋ฆฌ์–ธ ๋ฐ˜ํ™˜๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์—ฐ์†์ ์œผ๋กœ ํ˜ธ์ถœ์ด ๋ถˆ๊ฐ€๋Šฅ

 

์š”์†Œ ์ผ๊ด„ ์‚ญ์ œ

Map ๊ฐ์ฒด์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์ผ๊ด„ ์‚ญ์ œํ•˜๋ ค๋ฉด Map.prototype.clear ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ, clear ๋ฉ”์„œ๋“œ๋Š” ์–ธ์ œ๋‚˜ undefined๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

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

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

map.clear();
console.log(map); // Map(0) {}

 

์š”์†Œ ์ˆœํšŒ

Map ๊ฐ์ฒด๋ฅผ ์ˆœํšŒํ•˜๋ ค๋ฉด Map.prototype.forEach ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ, 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"
}
designer {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']]);

// Map ๊ฐ์ฒด๋Š” Map.prototype์˜ Symbol.iterator ๋ฉ”์„œ๋“œ๋ฅผ ์ƒ์†๋ฐ›๋Š” ์ดํ„ฐ๋Ÿฌ๋ธ”์ด๋‹ค.
console.log(Symbol.iterator in map); // true

// ์ดํ„ฐ๋Ÿฌ๋ธ”์ธ Map ๊ฐ์ฒด๋Š” for...of ๋ฌธ์œผ๋กœ ์ˆœํšŒํ•  ์ˆ˜ ์žˆ๋‹ค.
for (const entry of map) {
  console.log(entry); // [{name: "Lee"}, "developer"]  [{name: "Kim"}, "designer"]
}

// ์ดํ„ฐ๋Ÿฌ๋ธ”์ธ Map ๊ฐ์ฒด๋Š” ์Šคํ”„๋ ˆ๋“œ ๋ฌธ๋ฒ•์˜ ๋Œ€์ƒ์ด ๋  ์ˆ˜ ์žˆ๋‹ค.
console.log([...map]);
// [[{name: "Lee"}, "developer"], [{name: "Kim"}, "designer"]]

// ์ดํ„ฐ๋Ÿฌ๋ธ”์ธ Map ๊ฐ์ฒด๋Š” ๋ฐฐ์—ด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํ• ๋‹น์˜ ๋Œ€์ƒ์ด ๋  ์ˆ˜ ์žˆ๋‹ค.
const [a, b] = map;
console.log(a, b); // [{name: "Lee"}, "developer"]  [{name: "Kim"}, "designer"]

 

Map ๊ฐ์ฒด๋Š” ์ดํ„ฐ๋Ÿฌ๋ธ”์ด๋ฉด์„œ ๋™์‹œ์— ์ดํ„ฐ๋ ˆ์ดํ„ฐ์ธ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

 

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']]);

// Map.prototype.keys๋Š” Map ๊ฐ์ฒด์—์„œ ์š”์†Œํ‚ค๋ฅผ ๊ฐ’์œผ๋กœ ๊ฐ–๋Š” ์ดํ„ฐ๋ ˆ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
for (const key of map.keys()) {
  console.log(key); // {name: "Lee"} {name: "Kim"}
}

// Map.prototype.values๋Š” Map ๊ฐ์ฒด์—์„œ ์š”์†Œ๊ฐ’์„ ๊ฐ’์œผ๋กœ ๊ฐ–๋Š” ์ดํ„ฐ๋ ˆ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
for (const value of map.values()) {
  console.log(value); // developer designer
}

// Map.prototype.entries๋Š” Map ๊ฐ์ฒด์—์„œ ์š”์†Œํ‚ค์™€ ์š”์†Œ๊ฐ’์„ ๊ฐ’์œผ๋กœ ๊ฐ–๋Š” ์ดํ„ฐ๋ ˆ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
for (const entry of map.entries()) {
  console.log(entry); // [{name: "Lee"}, "developer"]  [{name: "Kim"}, "designer"]
}

 

์ด์ฒ˜๋Ÿผ Map ๊ฐ์ฒด๋Š” ์š”์†Œ์˜ ์ˆœ์„œ์— ์˜๋ฏธ๋ฅผ ๊ฐ–์ง€ ์•Š์ง€๋งŒ, ์ดํ„ฐ๋Ÿฌ๋ธ”์˜ ์ˆœํšŒ์™€ ํ˜ธํ™˜์„ฑ์„ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์ˆœํšŒ ์ˆœ์„œ๋Š” ์š”์†Œ๊ฐ€ ์ถ”๊ฐ€๋œ ์ˆœ์„œ๋ฅผ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค.


[์ถœ์ฒ˜] ๋ชจ๋˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ Deep Dive

https://wikibook.co.kr/mjs/

 

๋ชจ๋˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ Deep Dive: ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ธฐ๋ณธ ๊ฐœ๋…๊ณผ ๋™์ž‘ ์›๋ฆฌ

269๊ฐœ์˜ ๊ทธ๋ฆผ๊ณผ ์›๋ฆฌ๋ฅผ ํŒŒํ—ค์น˜๋Š” ์„ค๋ช…์œผ๋กœ ‘์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ธฐ๋ณธ ๊ฐœ๋…๊ณผ ๋™์ž‘ ์›๋ฆฌ’๋ฅผ ์ดํ•ดํ•˜์ž! ์›นํŽ˜์ด์ง€์˜ ๋‹จ์ˆœํ•œ ๋ณด์กฐ ๊ธฐ๋Šฅ์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ์ œํ•œ์ ์ธ ์šฉ๋„๋กœ ํƒœ์–ด๋‚œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ๊ณผ๋„

wikibook.co.kr