โœ๐Ÿป [Code Camp_TIL] 7์ผ์ฐจ: Map & Filter

code_Jยท2023๋…„ 3์›” 23์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
11/41
post-thumbnail

Map


์ง€๊ธˆ๊นŒ์ง€๋Š” ๋ฐ˜๋ณต์— for๋ฌธ์„ ์‚ฌ์šฉํ–ˆ์—ˆ๋Š”๋ฐ, ์‹ค๋ฌด์—์„œ๋Š” map๊ณผ filter๋ฅผ ๋” ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•œ๋‹ค. map๊ณผ filter๋Š” ๋ชจ๋‘ ๋ฐฐ์—ด์˜ ๋‚ด์žฅํ•จ์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฐฐ์—ด๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.


map ์‚ฌ์šฉ๋ฐฉ๋ฒ•


const classmates = ["์ฒ ์ˆ˜", "์˜ํฌ", "ํ›ˆ์ด"]
classmates.map((el) => (el + "์–ด๋ฆฐ์ด"))
// ["์ฒ ์ˆ˜์–ด๋ฆฐ์ด", "์˜ํฌ์–ด๋ฆฐ์ด", "ํ›ˆ์ด์–ด๋ฆฐ์ด"]

map์€ ๋ฐฐ์—ด ์•ˆ์˜ ๊ฐ’๋“ค์„ ํ•œ๋ฒˆ์— ๋ณ€ํ˜•์‹œํ‚ฌ ๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ํŽธ๋ฆฌํ•˜๋‹ค. ์œ„์˜ ์ฝ”๋“œ๋Š” map์„ ์‚ฌ์šฉํ•˜์—ฌ classmates๋ผ๋Š” ๋ฐฐ์—ด ์•ˆ์˜ ๋ชจ๋“  ์š”์†Œ ๋’ค์— ๊ฐ๊ฐ "์–ด๋ฆฐ์ด"๋ผ๋Š” ๋ฌธ์ž์—ด์„ ๋”ํ•œ ๊ฒƒ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.

์—ฌ๊ธฐ์„œ el์€ classmates์˜ ์š”์†Œ๋“ค์ด ๋“ค์–ด๊ฐˆ ํŒŒ๋ผ๋ฏธํ„ฐ(๋งค๊ฐœ๋ณ€์ˆ˜)์ด๋‹ค. ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์€ ๋งˆ์Œ๋Œ€๋กœ ์ •ํ•ด๋„ ๋œ๋‹ค.


**๊ฐ์ฒด**๊ฐ€ ์›์†Œ์ธ ๋ฐฐ์—ด์—์„œ๋Š” `map`์„ ์–ด๋–ค ์‹์œผ๋กœ ์ด์šฉํ•  ์ˆ˜ ์žˆ์„๊นŒ? classmate ๋ฐฐ์—ด์— 3๊ฐœ์˜ ๊ฐ์ฒด๊ฐ€ ๋‹ด๊ฒจ์žˆ๊ณ , name key์˜ ๊ฐ’ ๋’ค์— ๊ฐ๊ฐ "์–ด๋ฆฐ์ด"๋ฅผ ๋”ํ•˜๊ณ , school์ด๋ผ๋Š” ์ƒˆ๋กœ์šด key์™€ "๋–ก์žŽ์œ ์น˜์›"์ด๋ผ๋Š” value๋ฅผ ๋”ํ•ด๋ณด์ž.
const classmate = [{ name: "์ฒ ์ˆ˜" }, { name: "์˜ํฌ" }, { name: "ํ›ˆ์ด" }];

  classmate.map((item) => ({
    name: item.name + "์–ด๋ฆฐ์ด",
    school: "๋–ก์žŽ์œ ์น˜์›",
  }));

	// ๊ฒฐ๊ณผ
[
			    { name: "์ฒ ์ˆ˜์–ด๋ฆฐ์ด", school: "๋–ก์žŽ์œ ์น˜์›" },
			    { name: "์˜ํฌ์–ด๋ฆฐ์ด", school: "๋–ก์žŽ์œ ์น˜์›" },
			    { name: "ํ›ˆ์ด์–ด๋ฆฐ์ด", school: "๋–ก์žŽ์œ ์น˜์›" },
			  ];



Map๊ณผ HTML ์—ฐ๊ฒฐ


Map์€ ์ปดํฌ๋„ŒํŠธ์˜ return๊ฐ’ ์•ˆ์—์„œ ๋งŽ์ด ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋Š”๋ฐ, ์ด ๊ฒฝ์šฐ์—๋Š” map์„ ์ค‘๊ด„ํ˜ธ๋กœ ๊ฐ์‹ธ์„œ ์‚ฌ์šฉํ•œ๋‹ค.


const FRUITS = [
  { number: 1, title: "๋ ˆ๋“œํ–ฅ" }, 
  { number: 2, title: "์ƒค์ธ๋จธ์Šค์ผ“" }, 
  { number: 3, title: "์‚ฐ์ฒญ๋”ธ๊ธฐ" },
  { number: 4, title: "ํ•œ๋ผ๋ด‰" },
  { number: 5, title: "์‚ฌ๊ณผ" },
  { number: 6, title: "์• ํ”Œ๋ง๊ณ " },
  { number: 7, title: "๋”ธ๊ธฐ" },
  { number: 8, title: "์ฒœํ˜œํ–ฅ" },
  { number: 9, title: "๊ณผ์ผ์„ ๋ฌผ์„ธํŠธ" },
  { number: 10, title: "๊ทค" },
];

FRUITS ๋ฐฐ์—ด ์•ˆ์˜ ๊ฐ ๊ฐ์ฒด๋“ค์„ ์•„๋ž˜์˜ ํ˜•์‹์ฒ˜๋Ÿผ ์ธ๊ธฐ๊ฒ€์ƒ‰์–ด ๋ชฉ๋ก์œผ๋กœ ๋งŒ๋“œ๋Š” ์ž‘์—…์„ ํ•ด๋ณด์ž.

1 ๋ ˆ๋“œํ–ฅ
2 ์ƒค์ธ๋จธ์Šค์ผ“
3 ์‚ฐ์ฒญ๋”ธ๊ธฐ
...

๋‹ค์Œ๊ณผ ๊ฐ™์ด map์„ ์‚ฌ์šฉํ•ด์ฃผ๋ฉด ๋œ๋‹ค. div ํƒœ๊ทธ๋กœ ์š”์†Œ๋“ค์„ ๋‚˜ํƒ€๋‚ด์ค„ ํ˜•ํƒœ๋ฅผ ๊ฐ์‹ธ์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ html๊ณผ ์—ฐ๊ฒฐํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

export default function MapFruitsPage() {
 
  return(
    <div>
        {FRUITS.map((el) => (
          <div>
            {el.number} {el.title}
          </div>
        ))}
    </div>
  )
}

Filter

Filter๋Š” ๋‹จ์–ด์—์„œ ์œ ์ถ”ํ•  ์ˆ˜ ์žˆ๋“ฏ์ด ํŠน์ • ์กฐ๊ฑด์„ filter๋กœ ์„ค์ •ํ•˜์—ฌ ๊ฐ์ฒด ํ˜น์€ ๋ฐฐ์—ด์˜ ๊ฐ ์›์†Œ๋ฅผ ๊ฒ€์‚ฌํ•˜์—ฌ ์กฐ๊ฑด์— ๋งž๋Š” ๊ฒƒ๋“ค๋งŒ ๊ฒฐ๊ณผ๊ฐ’์œผ๋กœ ๋„์ถœํ•˜๋Š” ๋งค์„œ๋“œ๋‹ค. ์•„๋ž˜์˜ ์ฝ”๋“œ์™€ ๊ฐ™์ด ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ง€์ •ํ•ด์ฃผ๊ณ , ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ™œ์šฉํ•ด์„œ filter ์†Œ๊ด„ํ˜ธ ์•ˆ์— ์กฐ๊ฑด์„ ์‚ฝ์ž…ํ•˜๋ฉด ๋œ๋‹ค.

const num = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

num.filter((item) => item <= 8);
// [1, 2, 3, 4, 5, 6, 7, 8]

map๊ณผ filter๋ฅผ ๋ชจ๋ฅผ ๋•Œ, for ๋ฌธ์„ ์ด์šฉํ•ด์„œ ์ผ์ผ์ด ์–ผ๋งˆ๋‚˜ ๋ฐ˜๋ณตํ•˜๊ณ  ์–ธ์ œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•ด์ค„์ง€๋ฅผ ์„ค์ •ํ•˜๋Š” ๊ณผ์ •์ด ๋ฒˆ๊ฑฐ๋กœ์› ๋Š”๋ฐ map๊ณผ filter๋ฅผ ํ™œ์šฉํ•ด๋ณด๋‹ˆ๊นŒ ํ›จ์”ฌ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ฐฐ์—ด ์•ˆ ์š”์†Œ๋“ค์„ ์ปจํŠธ๋กค ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ ๊ฐ™๋‹ค!

๋ฐฐ์—ด์„ ํ•œ๋ฒˆ์— ์กฐ์ž‘ํ•ด์ฃผ๊ณ , ํŠน์ • ์š”์†Œ๋“ค๋งŒ ์กฐ๊ฑด์„ ์„ค์ •ํ•ด ๊ณจ๋ผ์„œ ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๊ฐ’์„ ๋งŒ๋“ค์–ด๋‚ผ ์ˆ˜ ์žˆ๋Š” ๊ฐ•๋ ฅํ•œ ๋งค์„œ๋“œ์ž„์— ํ‹€๋ฆผ์—†๋‹ค. ์žŠ์ง€ ๋ง๊ณ  ๋ณต์Šตํ•˜์ž ๊ผญ..!



profile
Web FE ๊ฐœ๋ฐœ์ž ์ทจ์ค€์ƒ

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