JavaScript : ๐Ÿšฐ filter ์‚ฌ์šฉ๋ฒ•

haleyยท2022๋…„ 1์›” 25์ผ
11

JavaScript ๊ธฐ๋ณธ ๋ฌธ๋ฒ•

๋ชฉ๋ก ๋ณด๊ธฐ
1/2


์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๋ฐ์ดํ„ฐ ์ค‘์— ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋งŒ์„ ๋ฝ‘์•„๋‚ด๊ณ  ์‹ถ๋‹ค๋ฉด? ๐Ÿค”
์ด์ฒ˜๋Ÿผ ์–ด๋–ค ๋ฐ์ดํ„ฐ ์ง‘ํ•ฉ์—์„œ ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๊ฐ’๋งŒ ๋ฐ˜ํ™˜๋ฐ›๊ธฐ๋ฅผ ์›ํ•  ๋•Œ filter ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

โœ๏ธ filter ๋ž€?

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ filter ๋Š” ๋ฐฐ์—ด์— ์‚ฌ์šฉํ•˜๋ฉฐ, ์ฃผ์–ด์ง„ ํ•จ์ˆ˜๋ฅผ ๋งŒ์กฑํ•˜๋Š” ๋ชจ๋“  ์š”์†Œ๋ฅผ ๋ชจ์•„ ์ƒˆ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด filter ๊ฐ€ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉ๋˜๋Š”์ง€ ๊ฐ„๋‹จํžˆ ์•Œ์•„๋ณด์ž.

let array = [3,5,11,0,9,'string'];
let result = array.filter((value) => value < 10)
console.log(result);

//output : array [3, 5, 0, 9]

์œ„์˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด, array๋ผ๋Š” ๋ฐฐ์—ด์ด ์žˆ๋‹ค. ์ด ๋ฐฐ์—ด์—์„œ 10๋ฏธ๋งŒ์˜ ๊ฐ’๋งŒ ์ถ”์ถœํ•˜๋ ค๊ณ  ํ•œ๋‹ค.
์ด๋ฅผ ์œ„ํ•ด ์šฐ์„  ๊ฒฐ๊ณผ๊ฐ’๋“ค์„ ๋‹ด์•„๋‘˜ result ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค. ๊ทธ ํ›„ ๊ธฐ์กด ๋ฐฐ์—ด์— .filter()๋ฅผ ๋ถ™์ด๊ณ  ๊ด„ํ˜ธ ์•ˆ์—๋Š” ํ•„ํ„ฐ์กฐ๊ฑด์ด ๋˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋„ฃ์–ด ์ฃผ์—ˆ๋‹ค.

๐Ÿ”จ filter ์‚ฌ์šฉ๋ฒ•

filter ๋ฅผ ์“ธ ๋•Œ, ์กฐ๊ฑด์‹์„ filter()์˜ ๊ด„ํ˜ธ ์•ˆ์— ์“ฐ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๊ณ  filter์™ธ๋ถ€์— ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

ํ•จ์ˆ˜๋ฅผ filter ๋‚ด์— ์ฃผ๊ธฐ

์œ„ ์‚ฌ์ง„์˜ #1 solution์„ ๋ณด๋ฉด, filter ๋ฉ”์„œ๋“œ์˜ ์กฐ๊ฑด์‹์„ ๊ด„ํ˜ธ ์•ˆ์— ๋ฐ”๋กœ ์ž‘์„ฑํ•ด ์ฃผ์—ˆ๋‹ค.
์ด ๋ฐฉ๋ฒ•์€ ์กฐ๊ฑด์‹์ด ๊ฐ„๋‹จํ•  ๊ฒฝ์šฐ ์œ ์šฉํ•˜๋ฉฐ, ์ฝ”๋“œ ๋ผ์ธ์„ ์ค„์ผ ์ˆ˜ ์žˆ๊ณ  ํ•œ ๋ˆˆ์— ๋ณด๊ธฐ ํŽธํ•œ ์žฅ์ ์ด ์žˆ๋‹ค.

ํ•จ์ˆ˜๋ฅผ filter ์™ธ๋ถ€์— ์‚ฌ์šฉ

์œ„ ์‚ฌ์ง„์˜ #2 solution ์„ ๋ณด๋ฉด, filter ์˜ ์กฐ๊ฑด์„ ์™ธ๋ถ€ํ•จ์ˆ˜๋กœ ์„ ์–ธํ•œ ํ›„ callbackํ•˜๊ณ  ์žˆ๋‹ค.
์ด ๋ฐฉ๋ฒ•์€ filter ์กฐ๊ฑด์‹์ด ๋ณต์žกํ•ด์งˆ ์ˆ˜๋ก ์ฝ”๋“œ ๊ด€๋ฆฌ์— ์œ ์šฉํ•  ๊ฒƒ ๊ฐ™๋‹ค.

๐Ÿงฉ Quiz

filter๋ฅผ ์ด์šฉํ•ด ๋‹ค์Œ ๋ฐฐ์—ด์—์„œ species๊ฐ€ 'mammalia'์ธ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ  ์ฝ˜์†”์— ์ถœ๋ ฅํ•ด ์ฃผ์„ธ์š”.

let animals = [
  { species: 'mammalia', name: "cat" },
  { species: 'reptiles', name: "lizard" },
  { species: 'amphibia', name: "flog" },
  { species: 'mammalia', name: "dog" },
  { species: 'amphibia', name: "salamander" }
];

์œ„ ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•ด์„œ ์šฐ์„  ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ์ €์žฅํ•  ๋ณ€์ˆ˜ result๋ฅผ ์„ ์–ธํ•œ๋‹ค.
๐Ÿ‘‰ let result

๊ทธ ํ›„ animals ๋ฐฐ์—ด์— filter ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
๐Ÿ‘‰ let result = animals.filter()

์ด์ œ filter์— ์กฐ๊ฑดํ•จ์ˆ˜๋ฅผ ์ค„ ๊ฒƒ์ด๋‹ค. ๊ทผ๋ฐ animals ๋ฐฐ์—ด์˜ ์š”์†Œ๋“ค์€ {} ์•ˆ์— ๋‹ด๊ฒจ์žˆ๋‹ค. ์ด๋Ÿฐ ์ค‘๊ด„ํ˜ธ ์•ˆ์˜ ์š”์†Œ๋ฅผ property๋ผ๊ณ  ํ•˜๋Š”๋ฐ, .์„ ์ฐ์–ด์„œ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.
๋˜ํ•œ species๊ฐ€ 'mammalia'์™€ ์ผ์น˜ํ•˜๋Š”์ง€ ๊ฒ€์‚ฌํ•˜๊ธฐ ์œ„ํ•ด ๋™๋“ฑ์—ฐ์‚ฐ์ž == ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿผ ์กฐ๊ฑด์‹์„ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
๐Ÿ‘‰ let result = animals.filter((value) => value.species == 'mammalia')

์ด๋ฅผ ์ฝ”๋“œ๋กœ ์ž‘์„ฑํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

let animals = [
  { species: 'mammalia', name: "cat" },
  { species: 'reptiles', name: "lizard" },
  { species: 'amphibia', name: "flog" },
  { species: 'mammalia', name: "dog" },
  { species: 'amphibia', name: "salamander" }
];

  let result = animals.filter((value) => value.species == 'mammalia')
  

console.log(result);

๊ฒฐ๊ณผ๊ฐ€ ์•„๋ž˜์™€ ๊ฐ™์ด ๋‚˜์˜ค๋ฉด ์„ฑ๊ณต์ด๋‹ค.

[
{ species: "mammalia", name: "cat" }, 
{ species: "mammalia", name: "dog" }
]
profile
๊ธฐ๋ก์žฅ

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

comment-user-thumbnail
2023๋…„ 4์›” 11์ผ

์„ค๋ช…์ด ์•„์ฃผ ์ž˜๋˜์–ด ์žˆ๋„ค์š” ๊ตฟ๊ตฟ !!

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ