๋ฐ˜์‘ํ˜•

TL;DR


๐Ÿ’ก ES6์— ๋„์ž…๋œ Set ๊ฐ์ฒด๋Š” ์ค‘๋ณต์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š” ๊ณ ์œ ํ•œ ๊ฐ’๋“ค์„ ์ €์žฅํ•˜๋Š” ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋‹ค. Set์€ ๋™์ผํ•œ ๊ฐ’์„ ๋‘ ๋ฒˆ ์ €์žฅํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ค‘๋ณต๋œ ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜๋ฉด ์ž๋™์œผ๋กœ ๋ฌด์‹œํ•œ๋‹ค. ๋˜ํ•œ ์‚ฝ์ž… ์ˆœ์„œ๋ฅผ ๊ธฐ์–ตํ•˜๊ณ , ์›์‹œํ˜•๊ณผ ์ฐธ์กฐํ˜• ๊ฐ’์„ ๋ชจ๋‘ ์ง€์›ํ•œ๋‹ค — ์ฐธ๊ณ  ํฌ์ŠคํŒ…

 

Firefox 127 ๋ฒ„์ „๋ถ€ํ„ฐ ๋ณ„๋„์˜ ํด๋ฆฌํ•„ ์—†์ด ํฌ๋กฌ(Chrome)์„ ํฌํ•จํ•œ ๋Œ€๋ถ€๋ถ„์˜ ์ฃผ์š” ๋ธŒ๋ผ์šฐ์ € ์—”์ง„์—์„œ ๊ต์ง‘ํ•ฉ, ํ•ฉ์ง‘ํ•ฉ, ์ฐจ์ง‘ํ•ฉ ๋“ฑ ์ง‘ํ•ฉ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ์ƒˆ๋กœ์šด Set ๋ฉ”์„œ๋“œ๋ฅผ ์ง€์›ํ•œ๋‹ค. ํฌ๋กœ์Šค ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ง€์›ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

  • A.intersection(B) : ๋‘ Set์ด ๊ณตํ†ต์œผ๋กœ ๊ฐ€์ง€๋Š” ์š”์†Œ๋ฅผ ํฌํ•จํ•œ ์ƒˆ๋กœ์šด Set ๋ฐ˜ํ™˜ — ๊ต์ง‘ํ•ฉ
  • A.union(B) : ๋‘ Set์ด ๊ฐ€์ง„ ๋ชจ๋“  ์š”์†Œ๋ฅผ ํฌํ•จํ•œ ์ƒˆ๋กœ์šด Set ๋ฐ˜ํ™˜ — ํ•ฉ์ง‘ํ•ฉ
  • A.difference(B) : A Set์—๋Š” ์žˆ์ง€๋งŒ B Set์—๋Š” ์—†๋Š” ์š”์†Œ๋ฅผ ํฌํ•จํ•œ ์ƒˆ๋กœ์šด Set ๋ฐ˜ํ™˜ — ์ฐจ์ง‘ํ•ฉ
  • A.symmetricDifference(B) : ๋‘ Set์ด ๊ณตํ†ต์œผ๋กœ ๊ฐ€์ง€๋Š” ์š”์†Œ๋ฅผ ์ œ์™ธํ•œ ์ƒˆ๋กœ์šด Set ๋ฐ˜ํ™˜ — ๋Œ€์นญ ์ฐจ์ง‘ํ•ฉ
  • A.isSubsetOf(B) : A Set์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ B Set์ด ํฌํ•จํ•˜๋Š”์ง€ ์—ฌ๋ถ€ — A๊ฐ€ B์˜ ๋ถ€๋ถ„ ์ง‘ํ•ฉ์ธ์ง€ ์—ฌ๋ถ€
  • A.isSupersetOf(B) : A Set์ด B Set์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ํฌํ•จํ•˜๋Š”์ง€ ์—ฌ๋ถ€ — A๊ฐ€ B์˜ ์ƒ์œ„ ์ง‘ํ•ฉ์ธ์ง€ ์—ฌ๋ถ€
  • A.isDisjointFrom(B) : ๋‘ Set์ด ๊ณตํ†ต ์š”์†Œ๋ฅผ ๊ฐ€์ง€์ง€ ์•Š๋Š”์ง€ ์—ฌ๋ถ€ — ์„œ๋กœ์†Œ ์ง‘ํ•ฉ ์—ฌ๋ถ€

 

 

A.intersection(B) | ๊ต์ง‘ํ•ฉ


์ด๋ฏธ์ง€ ์ถœ์ฒ˜ MDN

  • ์„ค๋ช… : ๋‘ Set์ด ๊ณตํ†ต์œผ๋กœ ๊ฐ€์ง€๋Š” ์š”์†Œ๋ฅผ ํฌํ•จํ•œ ์ƒˆ๋กœ์šด Set ๋ฐ˜ํ™˜
  • ๋ฐ˜ํ™˜๊ฐ’ : Set
  • ์ˆ˜ํ•™์  ํ‘œ๊ธฐ : ๐ด∩๐ต — A์™€ B๊ฐ€ ๋ชจ๋‘ ๊ฐ€์ง€๋Š” ์š”์†Œ
  • ์ฐธ๊ณ  ๋งํฌ : MDN | ํ™œ์šฉ ์˜ˆ์‹œ

 

const odds = new Set([1, 3, 5, 7, 9]);
const squares = new Set([1, 4, 9]);
console.log(odds.intersection(squares)); // Set(2) { 1, 9 }

 

 

A.union(B) | ํ•ฉ์ง‘ํ•ฉ


์ด๋ฏธ์ง€ ์ถœ์ฒ˜ MDN

  • ์„ค๋ช… : ๋‘ Set์ด ๊ฐ€์ง„ ๋ชจ๋“  ์š”์†Œ๋ฅผ ํฌํ•จํ•œ ์ƒˆ๋กœ์šด Set ๋ฐ˜ํ™˜
  • ๋ฐ˜ํ™˜๊ฐ’ : Set
  • ์ˆ˜ํ•™์  ํ‘œ๊ธฐ : ๐ด∪๐ต — A์™€ B์˜ ๋ชจ๋“  ์š”์†Œ
  • ์ฐธ๊ณ  ๋งํฌ : MDN | ํ™œ์šฉ ์˜ˆ์‹œ

 

const evens = new Set([2, 4, 6, 8]);
const squares = new Set([1, 4, 9]);
console.log(evens.union(squares)); // Set(6) { 2, 4, 6, 8, 1, 9 }

 

 

A.difference(B) | ์ฐจ์ง‘ํ•ฉ


์ด๋ฏธ์ง€ ์ถœ์ฒ˜ MDN

  • ์„ค๋ช… : A Set์—๋Š” ์žˆ์ง€๋งŒ B Set์—๋Š” ์—†๋Š” ์š”์†Œ๋ฅผ ํฌํ•จํ•œ ์ƒˆ๋กœ์šด Set ๋ฐ˜ํ™˜ (A Set์—๋งŒ ์žˆ๋Š” ์š”์†Œ)
  • ๋ฐ˜ํ™˜๊ฐ’ : Set
  • ์ˆ˜ํ•™์  ํ‘œ๊ธฐ : ๐ดโˆ–๐ต — A์—์„œ B๋ฅผ ๋บ€ ์š”์†Œ
  • ์ฐธ๊ณ  ๋งํฌ : MDN | ํ™œ์šฉ ์˜ˆ์‹œ

 

const odds = new Set([1, 3, 5, 7, 9]);
const squares = new Set([1, 4, 9]);
console.log(odds.difference(squares)); // Set(3) { 3, 5, 7 }

 

 

A.symmetricDifference(B) | ๋Œ€์นญ ์ฐจ์ง‘ํ•ฉ


์ด๋ฏธ์ง€ ์ถœ์ฒ˜ MDN

  • ์„ค๋ช… : ๋‘ Set์ด ๊ณตํ†ต์œผ๋กœ ๊ฐ€์ง€๋Š” ์š”์†Œ๋ฅผ ์ œ์™ธํ•œ ์ƒˆ๋กœ์šด Set ๋ฐ˜ํ™˜
  • ๋ฐ˜ํ™˜๊ฐ’ : Set
  • ์ˆ˜ํ•™์  ํ‘œ๊ธฐ : (๐ดโˆ–๐ต)∪(๐ตโˆ–๐ด) — A์—์„œ B๋ฅผ ๋บ€ ์š”์†Œ + B์—์„œ A๋ฅผ ๋บ€ ์š”์†Œ
  • ์ฐธ๊ณ  ๋งํฌ : MDN | ํ™œ์šฉ ์˜ˆ์‹œ

 

const evens = new Set([2, 4, 6, 8]);
const squares = new Set([1, 4, 9]);
console.log(evens.symmetricDifference(squares)); // Set(5) { 2, 6, 8, 1, 9 }

 

 

A.isSubsetOf(B) | ๋ถ€๋ถ„ ์ง‘ํ•ฉ


์ด๋ฏธ์ง€ ์ถœ์ฒ˜ MDN

  • ์„ค๋ช… : A Set์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ B Set์ด ํฌํ•จํ•˜๋Š”์ง€ ์—ฌ๋ถ€ (A๊ฐ€ B์˜ ๋ถ€๋ถ„ ์ง‘ํ•ฉ์ธ์ง€ ์—ฌ๋ถ€)
  • ๋ฐ˜ํ™˜๊ฐ’ : Boolean
  • ์ˆ˜ํ•™์  ํ‘œ๊ธฐ : ๐ด⊆๐ต — B๋Š” A์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ๊ฐ€์ง
  • ์ฐธ๊ณ  ๋งํฌ : MDN

 

// fours๋Š” evens์˜ ๋ถ€๋ถ„ ์ง‘ํ•ฉ
const fours = new Set([4, 8, 12, 16]);
const evens = new Set([2, 4, 6, 8, 10, 12, 14, 16, 18]);
console.log(fours.isSubsetOf(evens)); // true

// odds์—” 2๊ฐ€ ์—†์œผ๋ฏ€๋กœ primes๋Š” odds์˜ ๋ถ€๋ถ„ ์ง‘ํ•ฉ์ด ์•„๋‹˜
const primes = new Set([2, 3, 5, 7, 11, 13, 17, 19]);
const odds = new Set([3, 5, 7, 9, 11, 13, 15, 17, 19]);
console.log(primes.isSubsetOf(odds)); // false

// A, B ์ง‘ํ•ฉ์ด ๋™์ผํ•˜๋‹ค๋ฉด(๋“ฑ๊ฐ€ ์ง‘ํ•ฉ) ์„œ๋กœ์˜ ๋ถ€๋ถ„ ์ง‘ํ•ฉ
const set1 = new Set([1, 2, 3]);
const set2 = new Set([1, 2, 3]);
console.log(set1.isSubsetOf(set2)); // true
console.log(set2.isSubsetOf(set1)); // true

 

 

A.isSupersetOf(B) | ์ƒ์œ„ ์ง‘ํ•ฉ


์ด๋ฏธ์ง€ ์ถœ์ฒ˜ MDN

  • ์„ค๋ช… : A Set์ด B Set์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ํฌํ•จํ•˜๋Š”์ง€ ์—ฌ๋ถ€ (A๊ฐ€ B์˜ ์ƒ์œ„ ์ง‘ํ•ฉ์ธ์ง€ ์—ฌ๋ถ€)
  • ๋ฐ˜ํ™˜๊ฐ’ : Boolean
  • ์ˆ˜ํ•™์  ํ‘œ๊ธฐ : ๐ด⊇๐ต — A๋Š” B์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ๊ฐ€์ง
  • ์ฐธ๊ณ  ๋งํฌ : MDN  

 

// evens๋Š” fours์˜ ์ƒ์œ„ ์ง‘ํ•ฉ
const evens = new Set([2, 4, 6, 8, 10, 12, 14, 16, 18]);
const fours = new Set([4, 8, 12, 16]);
console.log(evens.isSupersetOf(fours)); // true

// primes์—” 15๊ฐ€ ์—†์œผ๋ฏ€๋กœ primes๋Š” odds์˜ ์ƒ์œ„ ์ง‘ํ•ฉ์ด ์•„๋‹˜
const primes = new Set([2, 3, 5, 7, 11, 13, 17, 19]);
const odds = new Set([3, 5, 7, 9, 11, 13, 15, 17, 19]);
console.log(odds.isSupersetOf(primes)); // false

// A, B ์ง‘ํ•ฉ์ด ๋™์ผํ•˜๋‹ค๋ฉด(๋“ฑ๊ฐ€ ์ง‘ํ•ฉ) ์„œ๋กœ์˜ ์ƒ์œ„ ์ง‘ํ•ฉ
const set1 = new Set([1, 2, 3]);
const set2 = new Set([1, 2, 3]);
console.log(set1.isSupersetOf(set2)); // true
console.log(set2.isSupersetOf(set1)); // true

 

 

A.isDisjointFrom(B) | ์„œ๋กœ์†Œ


์ด๋ฏธ์ง€ ์ถœ์ฒ˜ MDN

  • ์„ค๋ช… : ๋‘ Set์ด ๊ณตํ†ต ์š”์†Œ๋ฅผ ๊ฐ€์ง€์ง€ ์•Š๋Š”์ง€ ์—ฌ๋ถ€ (์„œ๋กœ์†Œ ์—ฌ๋ถ€)
  • ๋ฐ˜ํ™˜๊ฐ’ : Boolean
  • ์ˆ˜ํ•™์  ํ‘œ๊ธฐ : ๐ด∩๐ต=∅ — A, B ๊ณตํ†ต ์š”์†Œ ์—†์Œ
  • ์ฐธ๊ณ  ๋งํฌ : MDN

 

// primes, squares ๊ณตํ†ต ์š”์†Œ๊ฐ€ ์—†์œผ๋ฏ€๋กœ ์„œ๋กœ์†Œ
const primes = new Set([2, 3, 5, 7, 11, 13, 17, 19]);
const squares = new Set([1, 4, 9, 16]);
console.log(primes.isDisjointFrom(squares)); // true

// 4, 9, 16 ๊ณตํ†ต ์š”์†Œ๋ฅผ ๊ฐ€์ง€๋ฏ€๋กœ ์„œ๋กœ์†Œ ์•„๋‹˜
const composites = new Set([4, 6, 8, 9, 10, 12, 14, 15, 16, 18]);
const squares = new Set([1, 4, 9, 16]);
console.log(composites.isDisjointFrom(squares)); // false

 

 

๋ ˆํผ๋Ÿฐ์Šค


 

New JavaScript Set methods | MDN Blog

New JavaScript Set methods are landing across browsers. Learn about sets, how you can use these methods to compare different sets, create new sets with specific properties, and more.

developer.mozilla.org

 

 


๊ธ€ ์ˆ˜์ •์‚ฌํ•ญ์€ ๋…ธ์…˜ ํŽ˜์ด์ง€์— ๊ฐ€์žฅ ๋น ๋ฅด๊ฒŒ ๋ฐ˜์˜๋ฉ๋‹ˆ๋‹ค. ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•ด ์ฃผ์„ธ์š”
๋ฐ˜์‘ํ˜•