๐Ÿ‘‰๐Ÿป JS : ๋ฐฐ์—ด ํ•จ์ˆ˜ ๋ชจ์Œ

sebinnnnnยท2023๋…„ 2์›” 2์ผ
0

JavaScript ๊ธฐ์ดˆ

๋ชฉ๋ก ๋ณด๊ธฐ
7/8
post-thumbnail

1๏ธโƒฃย forEach( )

: ๋‹จ์ˆœํ•œ for๋ฌธ๊ณผ ๋™์ผํ•˜๊ฒŒ ์ž‘๋™ํ•˜๋ฉฐ return์„ ํ•˜์ง€ ์•Š๋Š”๋‹ค.
โ†’ ๋ฐฐ์—ด์— ์žˆ๋Š” ์š”์†Œ๋“ค์„ ํ•˜๋‚˜์”ฉ ๊ฐ€์ ธ์˜จ๋‹ค๋Š” ์˜๋ฏธ! โ†’ ๋ฐ˜ํ™˜๊ฐ’ ์—†์ด ๋‹จ์ˆœ ์ถœ๋ ฅ์ผ ๋•Œ ๋งŽ์ด ์‚ฌ์šฉ

let numbers = [1, 2, 3, 4, 5]

number.forEach((number)=>console.log(number))
// 1, 2, 3, 4, 5 ์ถœ๋ ฅ

2๏ธโƒฃย map( )

: forEach()์™€๋Š” ๋‹ฌ๋ฆฌ ๋ฐ˜ํ™˜๊ฐ’ return์„ ํ•˜๊ณ , ๋ฐ˜ํ™˜๋˜๋Š” ๊ฐ’์„ ๋ฐฐ์—ด์— ๋‹ด๋Š”๋‹ค.
โ†’ forEach๋Š” ๋‹จ์ˆœ string ํƒ€์ž…์œผ๋กœ ๊ฐ’์„ ์ถœ๋ ฅ / map()์€ ๋ฐฐ์—ด์— ๋‹ด์•„ ๋ฐ˜ํ™˜

let numbers = [1, 2, 3, 4, 5]

let numer = numbers.map(num => {return num})
// [1, 2, 3, 4, 5] ์ถœ๋ ฅ

3๏ธโƒฃย filter( )

: ํŠน์ • ์กฐ๊ฑด์— ๋งŒ์กฑํ•˜๋Š” ์š”์†Œ๋“ค๋งŒ ๋ฐฐ์—ด์— ๋‹ด์•„์„œ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
โ†’ ๋ฐฐ์—ด์— ๋‹ด์•„์„œ ๋ฐ˜ํ™˜(map) + ์กฐ๊ฑด์— ์ถฉ์กฑํ•˜๋Š” = true์— ํ•ด๋‹นํ•˜๋Š” ์š”์†Œ๋“ค๋งŒ

let numbers = [1, 2, 3, 4, 5]

let numer = numbers.filter(num => num >= 3)
// [3, 4, 5] ์ถœ๋ ฅ

4๏ธโƒฃย some( )

: ํŠน์ • ์กฐ๊ฑด์— ๋งŒ์กฑํ•˜๋Š” ์š”์†Œ๊ฐ€ ํ•˜๋‚˜๋ผ๋„ ์žˆ๋‹ค๋ฉด true๋ฅผ ๋ฐ˜ํ™˜, ๋‹จ ํ•˜๋‚˜์˜ ์š”์†Œ๋„ ์กฐ๊ฑด์— ๋งŒ์กฑํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด false๋ฅผ ๋ฐ˜ํ™˜
โ†’ ํŠน์ • ์กฐ๊ฑด์— ๋”ฐ๋ผ ๋ฐ˜ํ™˜๊ฐ’์ด ๋‹ค๋ฅด๋‹ค + boolean ๊ฐ’์„ ๋ฐ˜ํ™˜

let numbers = [1, 2, 3, 4, 5]

let numer = numbers.some(num => num > 3)
// true ์ถœ๋ ฅ

let numer = numbers.some(num => num >= 6)
// false ์ถœ๋ ฅ

5๏ธโƒฃย every( )

: ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๋“ค์ด ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋ฉด true๋ฅผ ๋ฐ˜ํ™˜, ๋ชจ๋“  ์š”์†Œ๋“ค์ด ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜์ง€ ๋ชปํ•˜๋ฉด false๋ฅผ ๋ฐ˜ํ™˜
โ†’ some()๋Š” ๋‹จ ํ•˜๋‚˜์˜ ์š”์†Œ๋ผ๋„ ์กฐ๊ฑด์— ๋งŒ์กฑํ•˜๋ฉด true๋ฅผ ๋ฐ˜ํ™˜ํ–ˆ์ง€๋งŒ every()๋Š” ๋ชจ๋“  ์š”์†Œ๋“ค์ด ์กฐ๊ฑด์— ๋งŒ์กฑํ•ด์•ผ true๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

let numbers = [1, 2, 3, 4, 5]

let numer = numbers.every(num => num > 3)
// false ์ถœ๋ ฅ

let numer = numbers.filter(num => num >= 1)
// true ์ถœ๋ ฅ

6๏ธโƒฃย find( )

: ๋ฐฐ์—ด์˜ ์š”์†Œ ์ค‘์—์„œ ํŠน์ • ์กฐ๊ฑด์— ๋งŒ์กฑํ•˜๋Š” ์š”์†Œ๊ฐ€ ์žˆ๋‹ค๋ฉด 1๊ฐœ์˜ ์š”์†Œ๋งŒ ๋ฐ˜ํ™˜
โ†’ ์กฐ๊ฑด์— ๋งŒ์กฑํ•˜๋Š” ์š”์†Œ๋“ค์ด ์—ฌ๋Ÿฌ ๊ฐœ๋ผ๊ณ  ํ•ด๋„ ๊ทธ์ค‘ ํ•˜๋‚˜๋งŒ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

let numbers = [1, 2, 3, 4, 5]

let numer = numbers.filter(num => num >= 4)
// 4 ์ถœ๋ ฅ -> ์‹ค์ œ๋กœ๋Š” 4, 5๊ฐ€ ์กฐ๊ฑด ์ถฉ์กฑ

7๏ธโƒฃย findIndex( )

: ๋ฐฐ์—ด์˜ ์š”์†Œ ์ค‘์—์„œ ํŠน์ • ์กฐ๊ฑด์— ๋งŒ์กฑํ•˜๋Š” ์š”์†Œ๊ฐ€ ์žˆ๋‹ค๋ฉด 1๊ฐœ์˜ ์š”์†Œ์˜ ์ธ๋ฑ์Šค ๊ฐ’๋งŒ ๋ฐ˜ํ™˜
โ†’ ์กฐ๊ฑด์— ๋งŒ์กฑํ•˜๋Š” ์š”์†Œ๋“ค์ด ์—ฌ๋Ÿฌ ๊ฐœ๋ผ๊ณ  ํ•ด๋„ ๊ทธ์ค‘ ํ•˜๋‚˜๋งŒ, ์ฒซ ๋ฒˆ์งธ์— ์œ„์น˜ํ•œ ์š”์†Œ์˜ ์ธ๋ฑ์Šค ๊ฐ’๋งŒ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

let numbers = [1, 2, 3, 4, 5]

let numer = numbers.filter(num => num >= 4)
// 3 ์ถœ๋ ฅ -> ์‹ค์ œ๋กœ๋Š” [3], [4]๊ฐ€ ์กฐ๊ฑด ์ถฉ์กฑ
profile
๐Ÿ  ๋ธ”๋กœ๊ทธ ์ด์ „ ์ค‘ โ†’ https://medium.com/@sebinndev

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