Front-end πŸ”₯πŸ–€/JavaScript

[Java Script] μœ μΌν•œ 값을 μ €μž₯ν•˜λŠ” Set κ°μ²΄λž€?

stable_sound 2022. 5. 20. 02:32

Set 객체

  • μžλ£Œν˜•μ— 관계 없이 μœ μΌν•œ 값을 μ €μž₯ν•˜λŠ” 객체
  • 쀑볡을 μ œκ±°ν•œ κ°’λ“€μ˜ μ§‘ν•©

 

ꡬ문

let mySet = new Set()

 

속성

1. add : μƒˆλ‘œμš΄ μš”μ†Œλ₯Ό μΆ”κ°€

let mySet = new Set();

mySet.add(1); // Set{1}
mySet.add(5); // Set{1,5}
mySet.add(5); // Set{1,5} 쀑볡 κ°’ μ €μž₯ x , μœ μΌν•œ κ°’λ§Œ μ €μž₯ν•œλ‹€
mySet.add("hello"); // Set{1,5, 'hello'}

console.log(mySet);

2. has : νŠΉμ • 값을 κ°€μ§„ μš”μ†Œκ°€ μžˆλŠ”μ§€ 확인

// Set{ 1, 5, 'hello' }

console.log(mySet.has(1));  // true
console.log(mySet.has(2));  // false
console.log(mySet.has("hello"));  // true

3. delete : νŠΉμ • 값을 κ°–λŠ” μš”μ†Œλ₯Ό 제거

console.log(mySet.delete("hello"));  // true
console.log(mySet);  // Set {1, 5}

4. size : Set 객체 λ‚΄μ˜ μš”μ†Œ 개수 λ°˜ν™˜

// mySet {1, 5}

console.log(mySet.size); // 2

5. clear : Set κ°μ²΄μ—μ„œ λͺ¨λ“  μš”μ†Œ 제거

console.log(mySet.clear); // Set {}

 

Set을 λ°°μ—΄λ‘œ λ°”κΎΈλŠ” 방법

const mySet = new Set(["아이폰12", "κ°€λŸ­μ‹œ λ…ΈνŠΈ9"]);
const mySetArray = [...mySet];

console.log(mySet); // Set(2) { '아이폰12', 'κ°€λŸ­μ‹œ λ…ΈνŠΈ9' }
console.log(mySetArray); // ['아이폰12', 'κ°€λŸ­μ‹œ λ…ΈνŠΈ9']

Set을 λ°°μ—΄λ‘œ λ°”κΏ€ λ•Œμ—λŠ” μ „κ°œ μ—°μ‚°μž (spread operator) λ₯Ό μ΄μš©ν•œλ‹€.

 

* μ „κ°œ μ—°μ‚°μž (spread operator) 

: κ°μ²΄λ‚˜ λ°°μ—΄μ˜ 값을 λ‚˜μ—΄ν•΄μ£ΌλŠ” μ—°μ‚°μž. ν•˜λ‚˜ν•˜λ‚˜ λ„˜κ²¨μ€Œ. 이름 κ·ΈλŒ€λ‘œ 생각해보면 κ°μ²΄λ‚˜ 배열을 νŽΌμ³μ€€λ‹€.

 

const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const arr3 = [7, 8, 9];

const arrWrap = [...arr1, ...arr3];  // [1, 2, 3, 7, 8, 9]

console.log(arrWrap);

μ‰½κ²Œ μƒκ°ν•˜λ©΄.. κ°μ²΄λ‚˜ λ°°μ—΄μ˜ 값을 κ³ λŒ€λ‘œ λ³΅μ‚¬ν•΄μ™€μ„œ λΆ™μ—¬μ£ΌλŠ” λŠλ‚Œμ΄λ‹€.

 

 


set 객체λ₯Ό 처음 μ‚¬μš©ν•΄ λ³Έ λ°±μ€€ 문제

 

λ°±μ€€ 3052번 : λ‚˜λ¨Έμ§€

 

[node.js] λ°±μ€€ 3052번 : λ‚˜λ¨Έμ§€

문제 μ •μˆ˜ N개둜 이루어진 μˆ˜μ—΄ A와 μ •μˆ˜ Xκ°€ μ£Όμ–΄μ§„λ‹€. μ΄λ•Œ, Aμ—μ„œ X보닀 μž‘μ€ 수λ₯Ό λͺ¨λ‘ 좜λ ₯ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜μ‹œμ˜€. 두 μžμ—°μˆ˜ A와 Bκ°€ μžˆμ„ λ•Œ, A%BλŠ” Aλ₯Ό B둜 λ‚˜λˆˆ λ‚˜λ¨Έμ§€ 이닀. 예λ₯Ό λ“€μ–΄,

jeongeum1202.tistory.com

 


μ°Έκ³ 

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Set

 

Set - JavaScript | MDN

Set κ°μ²΄λŠ” μžλ£Œν˜•μ— 관계 없이 μ›μ‹œ κ°’κ³Ό κ°μ²΄ μ°Έμ‘° λͺ¨λ‘ μœ μΌν•œ 값을 μ €μž₯ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

developer.mozilla.org

https://medium.com/@khwsc1/%EC%9E%90%EB%B0%94%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8-es6-set%EC%97%90-%EB%8C%80%ED%95%B4-%EC%95%8C%EC%95%84%EB%B3%B4%EC%9E%90-9b7294dfba99

 

μžλ°”μŠ€ν¬λ¦½νŠΈ ES6β€Š—β€ŠSet에 λŒ€ν•΄ μ•Œμ•„λ³΄μž πŸŽ‰

ES6κ°€ λ“±μž₯ν•˜κΈ° μ΄μ „μ—λŠ” μžλ°”μŠ€ν¬λ¦½νŠΈ μžμ²΄λŠ” set을 κ΅¬ν˜„ν•˜κ³  μžˆμ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€. 근데, set이 λ­˜κΉŒμš”? Set은 데이터 νƒ€μž… μ€‘μ˜ ν•˜λ‚˜μΈλ°, μ€‘λ³΅λ˜λŠ” 값을 κ°€μ§€μ§€ μ•ŠλŠ” κ°’λ“€μ˜ λ¦¬μŠ€νŠΈμž…λ‹ˆλ‹€. 그리고

medium.com

https://miiingo.tistory.com/323

 

[Node.js] javascript: Set 객체 μ‚¬μš©λ²•

Set 객체 Set κ°μ²΄λŠ” ES6μ—μ„œ λ“±μž₯ν•œ μ€‘볡을 μ œκ±°ν•œ κ°’λ“€μ˜ μ§‘합이닀. Set κ°μ²΄ μ„ μ–Έ //new Set([iterable]); let mySet = new Set(); Set κ°μ²΄ μ‚¬μš© νŠΉμ • μš”μ†Œ μΆ”κ°€: add Set κ°μ²΄μ— μ£Όμ–΄μ§„ κ°’..

miiingo.tistory.com