[Java Script] μ μΌν κ°μ μ μ₯νλ Set κ°μ²΄λ?
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 κ°μ²΄λ₯Ό μ²μ μ¬μ©ν΄ λ³Έ λ°±μ€ λ¬Έμ
[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
μλ°μ€ν¬λ¦½νΈ 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