[React] ํ”„๋กœ์ ํŠธ ๋ฆฌ์ŠคํŠธ ํ•„ํ„ฐ๋ง ์—ฌ์ •(2) - 3์ค‘ ์ค‘์ฒฉ ๊ฐ์ฒด
728x90

Today, What I learned?

์ง€๋‚œ ๊ธ€์ด ๊ธธ์–ด์ ธ์„œ ์ด์–ด์„œ ์ž‘์„ฑํ•˜๋Š” ํ”„๋กœ์ ํŠธ ๋ฆฌ์ŠคํŠธ ํ•„ํ„ฐ๋ง ์—ฌ์ •~

์˜ค๋Š˜์€ 3์ค‘ ์ค‘์ฒฉ ๊ฐ์ฒด์˜ ๊ฐ’์„ ํ†ตํ•ด ํ”„๋กœ์ ํŠธ๋ฅผ ํ•„ํ„ฐ๋งํ•˜๋Š” ๊ณผ์ •์— ๋Œ€ํ•ด ๊ธฐ๋กํ•œ๋‹ค.

์—ฌํƒœ๊ป.. ์ค‘์ฒฉ ๊ฐ์ฒด๊นŒ์ง„ ๊ฒช์–ด๋ดค์ง€๋งŒ 3์ค‘์œผ๋กœ ์ค‘์ฒฉ๋œ.. ๊ทธ๋Ÿฌ๋‹ˆ๊นŒ ์ค‘์ฒฉ์˜ ์ค‘์ฒฉ์˜ ์ค‘์ฒฉ ๊ฐ์ฒด๋Š” ์ฒ˜์Œ์ด๋ผ ์‚ฝ์งˆ์„ ์ข€ ํ–ˆ๋‹ค.

๊ทธ๋ž˜๋„ Promise ๋ฐฐ์—ด๋ณด๋‹จ ๋นจ๋ฆฌ ํ•ด๊ฒฐํ•จ ใ…Ž

 

์ผ๋‹จ ์ตœ์ข…๋ณธ์€ ์ด๋ ‡๋‹ค. ๊ธฐ์œ ๋งˆ์Œ์œผ๋กœ gif๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค ^^..

ํŒŒ์ผ๋ช…: ํ•„ํ„ฐ๋งํ•ด๋ƒˆ๋‹ค.gif

 

 

์ด ๋…€์„์€ 3 ์ค‘์ฒฉ ๊ฐ์ฒด

์ง€์›ํ•œ, ์ฐธ์—ฌํ•œ ํ”„๋กœ์ ํŠธ์˜ ๊ฒฝ์šฐ ํ”„๋กœ์ ํŠธ ์•„์ด๋””๋กœ ๋ฌธ์„œ(๊ฐ์ฒด)๊ฐ€ ๋งŒ๋“ค์–ด์ง€๊ณ , ๊ทธ ์•ˆ ๊ฐ์ฒด์˜ recruited ๋ผ๋Š” ํ‚ค๊ฐ’์„ ํ†ตํ•ด ํŒ๋ณ„๋œ๋‹ค.

์•„๋ž˜์ฒ˜๋Ÿผ true/false์— ๋”ฐ๋ผ ๊ฐˆ๋ฆฐ๋‹ค.

 

 

appliedProjects๋ผ๋Š” ๊ฐ์ฒด ์•ˆ์˜ ๊ฐ ๋ฌธ์„œ ์•„์ด๋””๋ผ๋Š” ๊ฐ์ฒด ์•ˆ์˜ recruited....

๊ฒฐ๊ตญ ์ € ๊ฐ€์žฅ ์•ˆ์ชฝ ๊ฐ์ฒด์˜ true, false ๊ฐ’์„ ํŒ๋ณ„ํ•ด์„œ ๋ฌธ์„œ id๋ฅผ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์–ด ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•˜๋Š”.. ๊ทธ๋Ÿฐ ๊ณผ์ •์ธ ๊ฒƒ์ด๋‹ค.

 

 

Object.entries()

์ค‘์ฒฉ๊ฐ์ฒด๊นŒ์ง€๋Š” ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๋ ค๋ดค๊ธฐ ๋•Œ๋ฌธ์— Object.entries()๋ฅผ ์ด์šฉํ•ด์„œ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

  const getFilteredPidList = (pidList: PidListProps, category: string) => {
    const filteredPidList: string[] = [];

    for (const [key, value] of Object.entries(pidList.appliedProjects)) {
      console.log(value);
      ...
    }

    return filteredPidList;
  };

 

๋ฌธ์ œ๋Š” ์—ฌ๊ธฐ์„œ value๊ฐ€ ๋˜ ๊ฐ์ฒด๋ผ์„œ ์ € ๋…€์„์„ ํ†ตํ•ด ํŒ๋ณ„์„ ํ•ด์•ผ ํ•˜๋Š”๋ฐ!.. value๋Š” string์ด๋ผ๊ณ  ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๊ฐ€ ๊ณ„์† ์•Œ๋ ค์คฌ๋‹ค (ํ‘ํ‘)

 

 

value๋ฅผ ์ฝ˜์†”๋กœ ์ฐ์œผ๋ฉด ๊ฐ์ฒด์ธ๋ฐ ์ฝ”๋“œ ์ƒ์—์„œ๋Š” string ์ด๊ฒ ๋‹ค....

 

์ง€๊ธˆ ๊ธ€์„ ์ž‘์„ฑํ•˜๋‹ค mdn ๋ฌธ์„œ๋ฅผ ๋ณด๋‹ˆ ๋‹น์—ฐํ–ˆ๋‹ค. ๋ฌธ์ž์†์„ฑ [key, value] ์Œ์„ ๋ฐ˜ํ™˜ํ•˜๋‹ˆ๊นŒ value๋Š” ์ผ๋‹จ ๋ฌธ์ž์—ด๋กœ ์ธ์‹๋˜๊ฒ ์ง€..

 

 

 

์ตœ์ข… ํ•ด๊ฒฐ

๊ทธ๋Ÿผ value๋ฅผ ๊ฐ์ฒด๋กœ ๋งŒ๋“ค์–ด์•ผ์ง€ ๋ญ.. Object()๋กœ ์ƒˆ๋กœ์šด ๊ฐ์ฒด ์•ˆ์— value๋ฅผ ๋„ฃ์–ด์ฃผ์—ˆ๋”๋‹ˆ recruited ๊ฐ’์„ ์ž˜ ํŒ๋ณ„ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค. ์ตœ์ข…์ ์œผ๋กœ ์™„์„ฑ๋œ ํ•จ์ˆ˜๋Š” ์ด๋ ‡๋‹ค.

// ์ง€์›ํ•œ ํ”„๋กœ์ ํŠธ์ด๊ฑฐ๋‚˜ ์ฐธ์—ฌํ•œ ํ”„๋กœ์ ํŠธ์ผ ๊ฒฝ์šฐ appliedProjects ๋ฌธ์„œ์—์„œ pid ํ‚ค๊ฐ’์„ ํ•„ํ„ฐ๋ง ํ•ด์„œ ๊บผ๋‚ด๊ธฐ ์œ„ํ•œ ํ•จ์ˆ˜
  const getFilteredPidList = (pidList: PidListProps, category: string) => {
    const filteredPidList: string[] = [];

    for (const [key, value] of Object.entries(pidList.appliedProjects)) {
      if (category === 'appliedProjects') {
        Object(value).recruited === false ? filteredPidList.push(key) : null;
      } else {
        Object(value).recruited === true ? filteredPidList.push(key) : null;
      }
    }

    return filteredPidList;
  };

 

 

์ด ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋‹ค์‹œ ํ”„๋กœ์ ํŠธ ๋ฌธ์„œ ์•„์ด๋”” ๋ฐฐ์—ด์„ ์–ป์„ ์ˆ˜ ์žˆ์—ˆ๊ณ  1๋ฒˆ ๊ธ€์—์„œ ๋งŒ๋“ค์–ด๋‘” ํ•จ์ˆ˜๋กœ ๊ฐ’์„ ์–ป์–ด์˜ฌ ์ˆ˜ ์žˆ์—ˆ๋‹ค!~

๋‹ค์‹œ ๋˜ ์ƒˆ๋กญ๊ฒŒ ๋ฐฐ์šด ๊ฒƒ์ด ๋งŽ์€ ๊ธฐ๋Šฅ์ด์—ˆ๋‹ค.. ๐Ÿ˜‚

 

 

 

์ฐธ๊ณ 

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

728x90