230407 - set, map

๋ฐฑ์Šน์—ฐยท2023๋…„ 4์›” 10์ผ
1

๐Ÿšฉ javascript

set

๐Ÿ“ ์„ค๋ช…

  • ์ž๋ฃŒ๊ตฌ์กฐ ์ค‘ ๋ฐฐ์—ด์€ index(์ˆœ์„œ) ์ค‘์š”, item ์ค‘๋ณต ๊ฐ€๋Šฅ
  • ๊ทธ๋Ÿฌ๋‚˜ set์€ ๋ฐฐ์—ด๊ณผ ๋‹ฌ๋ฆฌ ์ˆœ์„œ(index)โŒ, item ์ค‘๋ณตโŒ


โœ’๏ธ ์ฝ”๋“œ ์ž‘์„ฑ

์ž…๋ ฅ

js

const set = new Set(["ํ•˜๋Š˜", "๋ฐ”๋‹ค", "์‚ฐ"]); // set ์ƒ์„ฑ
// new Set() // ๋นˆ ๊ฒƒ๋„ ์ƒ์„ฑ ๊ฐ€๋Šฅ // Set(0) {}
console.log("set? : ", set); // Set(3) { 'ํ•˜๋Š˜', '๋ฐ”๋‹ค', '์‚ฐ' }

console.log("set์˜ ์•„์ดํ…œ ๊ฐฏ์ˆ˜ ํ™•์ธ : ", set.size); // property
console.log("set์˜ ์•„์ดํ…œ ์กด์žฌ ํ™•์ธ(boolean) : ", set.has("์‚ฐ"));
console.log("set์˜ ์•„์ดํ…œ ์กด์žฌ ํ™•์ธ(boolean) : ", set.has(1004));
console.log();

// ์ˆœํšŒ, ๋ฐ˜๋ณต ๊ฐ€๋Šฅ(iterable)
set.forEach((item) => {
  console.log("forEach ์‚ฌ์šฉ : ", item);
});
console.log();

for (const item of set) {
  console.log("for of ์‚ฌ์šฉ : ", item);
}
console.log();

// ์ถ”๊ฐ€
set.add(1004);
console.log("1004 ์ถ”๊ฐ€ : ", set);
set.add(1004);
console.log("1004 ์ถ”๊ฐ€ : ", set); // ์•„์ดํ…œ ์ค‘๋ณต โŒ
console.log();

// ์‚ญ์ œ
set.delete("๋ฐ”๋‹ค");
console.log("๋ฐ”๋‹ค ์‚ญ์ œ : ", set);

// ์ „๋ถ€ ์‚ญ์ œ
set.clear();
console.log("์ „๋ถ€ ์‚ญ์ œ : ", set);
console.log();

// ์˜ค๋ธŒ์ ํŠธ
const obj1 = { name: "๐Ÿ”", price: 2500 };
const obj2 = { name: "๐Ÿฉ", price: 1000 };

const objs = new Set([obj1, obj2]); // 2๊ฐœ์˜ ๊ฐ์ฒด๊ฐ€ ๋“ค์–ด๊ฐ€๋Š” set์„ ๋งŒ๋“ฆ
console.log("objs? : ", objs);
console.log();

obj1.price = 1700; // ๊ฐ€๊ฒฉ์ธํ•˜
console.log("obj1 ๊ฐ€๊ฒฉ ์ธํ•˜ ํ›„ obj1? : ", obj1);
console.log("obj1 ๊ฐ€๊ฒฉ ์ธํ•˜ ํ›„ objs? : ", objs);
// *shallos copy(์–•์€ ๋ณต์‚ฌ) - set๋„ ๋ฐ”๋€œ

const obj3 = { name: "๐Ÿฉ", price: 1000 }; // obj2์™€ ๋˜‘๊ฐ™์€ ์•„์ดํ…œ์„ ์ถ”๊ฐ€
objs.add(obj3);
console.log("obj3 ์ถ”๊ฐ€? : ", objs);
// obj2์™€ ์•ˆ์˜ key์™€ value๋Š” ๊ฐ™์ง€๋งŒ ์ฃผ์†Œ๊ฐ€ ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ์ถ”๊ฐ€ ๊ฐ€๋Šฅ (์ค‘๋ณต์•„๋‹˜)

obj3.price = 1500; // ๊ฐ€๊ฒฉ์ธ์ƒ
console.log("obj3 ๊ฐ€๊ฒฉ ์ธ์ƒ ํ›„ objs : ", objs);
// obj3๋งŒ ๊ฐ€๊ฒฉ์ด ์˜ค๋ฆ„. obj2์™€ ์•„์˜ˆ ๋‹ค๋ฅธ ๊ฐ’์ด ๋จ

์ถœ๋ ฅ

  • ์ด๋ฏธ์ง€๋กœ ๋Œ€์ฒด

๐Ÿ”— ์ฐธ๊ณ  ๋งํฌ & ๋„์›€์ด ๋˜๋Š” ๋งํฌ






map

๐Ÿ“ ์„ค๋ช…

  • key, value๊ฐ’์œผ๋กœ ๊ตฌ์„ฑ. key๋Š” ์ค‘๋ณตโŒ
  • ๋ฐฐ์—ด๊ณผ ๋‹ฌ๋ฆฌ ์ˆœ์„œ(index)๊ฐ€ ์—†๊ณ , set๊ณผ ๋‹ฌ๋ฆฌ ์•„์ดํ…œ(value) ์ค‘๋ณต์ด ๊ฐ€๋Šฅ
  • ์˜ค๋ธŒ์ ํŠธ์™€ ๋น„์Šทํ•จ (key, value)
    • but, ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜๊ฐ€ map์ด ํ›จ์”ฌ ๋งŽ์Œ
    • ์˜ค๋ธŒ์ ํŠธ๋Š” ์•„์ดํ…œ์„ ํ˜ธ์ถœํ•  ๋•Œ obj[2]๋กœ ํ˜ธ์ถœํ•˜์ง€๋งŒ map์€ map.get()์œผ๋กœ ํ˜ธ์ถœํ•จ.


โœ’๏ธ ์ฝ”๋“œ ์ž‘์„ฑ

์ž…๋ ฅ

js

// Map ์ƒ์„ฑ์ž๋ฅผ ์ด์šฉํ•ด์„œ ๋งŒ๋“ฆ
// ๋ฐฐ์—ด ์•ˆ์— ๋˜ ๋‹ค๋ฅธ ๋ฐฐ์—ด์ด ์žˆ๋Š” ์Šคํƒ€์ผ
const map = new Map([
  ["key1", "๐ŸŽ"],
  ["key2", "๐ŸŒ"],
]);
console.log("Map ์ƒ์„ฑ์ž : ", map); // { 'key1' => '๐ŸŽ', 'key2' => '๐ŸŒ' }
console.log();

// ์‚ฌ์ด์ฆˆ ํ™•์ธ
console.log("map์˜ ์•„์ดํ…œ ๊ฐฏ์ˆ˜๋Š”? : ", map.size); // property
console.log();

// ์กด์žฌ ์—ฌ๋ถ€ - value๊ฐ€ ์•„๋‹ˆ๋ผ ๋ฌด์กฐ๊ฑด key๋กœ ํ™•์ธ
console.log("map์˜ key1 ์กด์žฌ ํ™•์ธ(boolean) : ", map.has("key1")); // mehtod
console.log("map์˜ key6 ์กด์žฌ ํ™•์ธ(boolean) : ", map.has("key6"));
console.log();

// ์ˆœํšŒ, ๋ฐ˜๋ณต
map.forEach((aa, bb, cc) => console.log(aa, bb, cc)); // ๐ŸŽ key1 Map(2) { 'key1' => '๐ŸŽ', 'key2' => '๐ŸŒ' }
// map์„ forEach๋กœ ๋Œ๋ฆฌ๋ฉด ์•ž์ด value ๋’ค๊ฐ€ key๊ฐ’
// (value, key, Map)
console.log();

console.log("map.values() : ", map.values());
console.log("map.keys() : ", map.keys());
console.log("map.entries() : ", map.entries());
console.log();

// ์ฐพ๊ธฐ
console.log("key1์˜ value๊ฐ’์€? : ", map.get("key1"));
console.log("key6์˜ value๊ฐ’์€? : ", map.get("key6")); // undefined
console.log();

// ์ถ”๊ฐ€
map.set(1004);
console.log("1004์ถ”๊ฐ€ : ", map); // 1004 => undefined - value๊ฐ€ ์—†์–ด์„œ undefined๋กœ ๋œธ

map.set("key5", "๐Ÿฅ");
console.log("ํ‚ค์œ„ ์ถ”๊ฐ€", map);
console.log();

// ์‚ญ์ œ
map.delete(1004)
console.log("1004 ์‚ญ์ œ", map);
console.log();

// ์ „์ฒด ์‚ญ์ œ
map.clear()
console.log("์ „์ฒด ์‚ญ์ œ", map);

์ถœ๋ ฅ

  • ์ด๋ฏธ์ง€๋กœ ๋Œ€์ฒด

๐Ÿ”— ์ฐธ๊ณ  ๋งํฌ & ๋„์›€์ด ๋˜๋Š” ๋งํฌ






์ง€๊ธˆ๊นŒ์ง€ ๋ฐฐ์šด ๋ฐฐ์—ด ์‘์šฉ ์ฝ”๋“œ


โœ’๏ธ ์ฝ”๋“œ ์ž‘์„ฑ

์ž…๋ ฅ

js

// ๋ฌธ์ œ1) ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์—์„œ ์ค‘๋ณต์„ ์ œ๊ฑฐ
const fruits = ["๐Ÿฅ", "๐Ÿฅฅ", "๐Ÿ‡", "๐ŸŠ", "๐Ÿ‹", "๐Ÿ‡", "๐Ÿ‡", "๐ŸŠ", "๐Ÿ‹"];
// const aa = new Set(fruits);
const aa = [...new Set(fruits)]; // spread
console.log(aa);
console.log();

// ๋ฌธ์ œ2) ์ฃผ์–ด์ง„ ๋‘ ๋ฐฐ์—ด์˜ ๊ณตํ†ต๋œ ์•„์ดํ…œ๋งŒ ๋‹ด๊ณ ์žˆ๋Š” ์„ธํŠธ๋ฅผ ๋งŒ๋“ค์–ด๋ผ
const array1 = [1, 2, 3, 4, 5, 2];
const array2 = [1, 2, 3, 3];

function commonFunc(arr1, arr2) {
  const newArray = arr1.filter((item) => arr2.includes(item));
  return [...new Set(newArray)]; // spread // [1, 2, 3]
}
console.log(commonFunc(array1, array2));

์ถœ๋ ฅ

  • ์ด๋ฏธ์ง€๋กœ ๋Œ€์ฒด






profile
๊ณต๋ถ€ํ•˜๋Š” ๋ฒจ๋กœ๊ทธ

0๊ฐœ์˜ ๋Œ“๊ธ€