forEach, map, filter, reduce, find

forEach, map, filter, reduce, find

🌎 forEach, map, filter, reduce, find

μœ„ λ„€ λ©”μ†Œλ“œλŠ” λ°°μ—΄μ—μ„œ 자주 μ“°μ΄λ©΄μ„œ λΉ„μŠ·ν•œ κΈ°λŠ₯을 ν•˜λŠ” λ©”μ„œλ“œλ“€μ΄λ‹€.


🌞 .forEach()

forEach() λ©”μ„œλ“œλŠ” 각 μš”μ†Œμ— λŒ€ν•˜μ—¬ μš”μ†Œκ°’κ³Ό 인덱슀λ₯Ό ν™œμš©ν•œ ν•¨μˆ˜λ₯Ό μž‘μ„±ν•˜μ—¬ μ‚¬μš©ν•˜λŠ” λ©”μ„œλ“œμ΄λ‹€. 이 λ•Œ forEachλŠ” return 값이 μ—†λ‹€.

undefined

const num = [1,2,3,4,5,6]; const init = 10; const num_forEach = num.forEach((val,cur) => { console.log(val, cur) return val + cur},init); console.log(num_forEach); // 1 0 // 2 1 // 3 2 // 4 3 // 5 4 // 6 5


---

## 🌞 .map()
map()은 λ°°μ—΄ 각 μš”μ†Œμ— λŒ€ν•˜μ—¬ μ£Όμ–΄μ§„ ν•¨μˆ˜λ₯Ό μˆ˜ν–‰ν•œ κ²°κ³Όλ₯Ό λͺ¨μ•„ μƒˆλ‘œμš΄ 배열을 λ°˜ν™˜ν•˜λŠ” λ©”μ„œλ“œμ΄λ‹€. 

μ‚¬μš© 방법은 3개의 λ§€κ°œλ³€μˆ˜μ™€ 이λ₯Ό μ‚¬μš©ν•˜λŠ” ν•¨μˆ˜λ₯Ό μž‘μ„±ν•˜μ—¬ μ‚¬μš©ν•œλ‹€.

μ•„λž˜μ™€ 같이 μ‚¬μš© κ°€λŠ₯ν•˜λ‹€.
> ```javascript
const num = [1,2,3,4,5,6];
const num_map = num.map((val,index,arr) => {
	console.log(val,index,arr);
	return val*2;
});
console.log(num_map);
//1 0 [ 1, 2, 3, 4, 5, 6 ]
//2 1 [ 1, 2, 3, 4, 5, 6 ]
//3 2 [ 1, 2, 3, 4, 5, 6 ]
//4 3 [ 1, 2, 3, 4, 5, 6 ]
//5 4 [ 1, 2, 3, 4, 5, 6 ]
//6 5 [ 1, 2, 3, 4, 5, 6 ]
//[ 2, 4, 6, 8, 10, 12 ]

각 λ©”κ°œ λ³€μˆ˜λŠ” μ•„λž˜μ™€ κ°™λ‹€.

- val : ν˜„μž¬ μš”μ†Œ
- index : ν˜„μž¬ μš”μ†Œμ˜ 인덱슀
- arr : map()을 ν˜ΈμΆœν•œ 원본 λ°°μ—΄

μ•„λž˜μ™€ 같이 λ§€κ°œλ³€μˆ˜λ‘œ λ°°μ—΄μ˜ 인자λ₯Ό λ°›κ³ , ν•΄λ‹Ή 인자λ₯Ό μ΄μš©ν•˜λŠ” 식을 μž‘μ„±ν•˜μ—¬ μ‚¬μš©ν•˜μ—¬ κ°„λ‹¨ν•˜κ²Œ λ‚˜νƒ€λ‚Ό μˆ˜λ„ μžˆλ‹€.
> ```javascript
const num = [1,2,3,4,5,6];
const num_map = num.map(val => val*2);
console.log(num_map);
//[ 2, 4, 6, 8, 10, 12 ]

---
## 🌞 .filter()
filter()λŠ” **λ°°μ—΄ 각 μš”μ†Œμ— λŒ€ν•΄μ„œ μ£Όμ–΄μ§„ ν•¨μˆ˜μ˜ 결괏값이 true인 μš”μ†Œλ₯Ό λͺ¨μ•„ μƒˆλ‘œμš΄ 배열을 λ°˜ν™˜ν•˜λŠ” λ©”μ„œλ“œ**이닀.

 map()μ—μ„œλŠ” 리턴 값이 λ‹€μ–‘ν•œ νƒ€μž…μœΌλ‘œ κ°€λŠ₯ν•˜μ§€λ§Œ, filterμ—μ„œλŠ” 였직 **boolean νƒ€μž…**만 λ¦¬ν„΄κ°’μœΌλ‘œ κ°€λŠ₯ν•˜λ‹€.
 
μ‚¬μš© 방법은 map()κ³Ό 같이 3개의 λ§€κ°œλ³€μˆ˜λ₯Ό κ°€μ§€κ³  이λ₯Ό μ‚¬μš©ν•˜λŠ” ν•¨μˆ˜λ₯Ό μž…λ ₯ν•˜μ—¬ μ‚¬μš©κ°€λŠ₯ν•˜λ‹€.
> ```javascript
const num = [1,2,3,4,5,6];
const num_filter = num.filter((val,index,arr) => {
	console.log(val,index,arr);
	return val % 2 === 0;
});
console.log(num_filter);
// 1 0 [ 1, 2, 3, 4, 5, 6 ]
// 2 1 [ 1, 2, 3, 4, 5, 6 ]
// 3 2 [ 1, 2, 3, 4, 5, 6 ]
// 4 3 [ 1, 2, 3, 4, 5, 6 ]
// 5 4 [ 1, 2, 3, 4, 5, 6 ]
// 6 5 [ 1, 2, 3, 4, 5, 6 ]
// [ 2, 4, 6 ]

μ•„λž˜μ™€ 같이 λ§€κ°œλ³€μˆ˜λ‘œ λ°°μ—΄μ˜ 인자λ₯Ό λ°›κ³ , ν•΄λ‹Ή 인자λ₯Ό μ΄μš©ν•˜λŠ” 식을 μž‘μ„±ν•˜μ—¬ μ‚¬μš©ν•˜μ—¬ κ°„λ‹¨ν•˜κ²Œ λ‚˜νƒ€λ‚Ό μˆ˜λ„ μžˆλ‹€.

> ```javascript
const num = [1,2,3,4,5,6];
const num_filter = num.filter(val => val % 2 ===0);
console.log(num_filter);
//[ 2, 4, 6 ]

---

## 🌞 .reduce()
reduce() λ©”μ„œλ“œλŠ” μœ„μ—μ„œ μ•Œμ•„λ³Έ μ„Έ λ©”μ„œλ“œλ³΄λ‹€ λ³΅μž‘ν•œ ν˜•νƒœλ₯Ό κ°€μ§€κ³  μžˆλ‹€. ν•˜μ§€λ§Œ μΆ©λΆ„ν•œ μ΄ν•΄ν•œλ‹€λ©΄ λ‹€μ–‘ν•œ μƒν™©μ—μ„œ ν™œμš©μ΄ κ°€λŠ₯ν•œ λ©”μ„œλ“œμ΄λ‹€.

 이 λ©”μ„œλ“œλŠ” λ°°μ—΄ 각 μš”μ†Œμ— λŒ€ν•˜μ—¬ μž…λ ₯ν•œ ν•¨μˆ˜λ₯Ό μ‹€ν–‰ν•˜κ³  리턴값을 λ‹€μŒ μš”μ†Œλ‘œ λ„˜κ²¨μ£Όλ©° λ§ˆμ§€λ§‰μ— ν•˜λ‚˜μ˜ 결괏값을 λ°˜ν™˜ν•œλ‹€. ν•˜λ‚˜μ˜ 결괏값을 λ°˜ν™˜ ν•œλ‹€λŠ” μ μ—μ„œ μœ„ μ„Έ λ©”μ„œλ“œμ™€ 차이가 μžˆλ‹€.

μ‚¬μš© 방법은 2개의 λ§€κ°œλ³€μˆ˜μ™€ μ‹€ν–‰ ν•¨μˆ˜, μ΄ˆκΈ°κ°’μ„ μž…λ ₯ν•˜μ—¬ μ‚¬μš©ν•œλ‹€. 이 λ•Œ μ΄ˆκΈ°κ°’μ€ 0으둜 default λ˜μ–΄ μžˆλ‹€.

> ```javascript
const num = [1,2,3,4,5,6];
const init = 10;
const num_reduce = num.reduce((val,cur,index,array) => {
    console.log(val, cur)
    return val + cur},init);
console.log(num_reduce);
// 10 1
// 11 2
// 13 3
// 16 4
// 20 5
// 25 6
// 31

- init : μ΄ˆκΈ°κ°’
- val : 전달값
- cur : ν˜„μž¬ μš”μ†Œ κ°’
- index : ν˜„μž¬ μš”μ†Œμ˜ 인덱슀 κ°’
- array : reduce()λ₯Ό ν˜ΈμΆœν•œ 원본 λ°°μ—΄

----
## 🌞.find()

find()** **λ©”μ„œλ“œλŠ” μ£Όμ–΄μ§„ νŒλ³„ ν•¨μˆ˜λ₯Ό λ§Œμ‘±ν•˜λŠ” 첫 번째 μš”μ†Œμ˜ **κ°’**을 λ°˜ν™˜ν•©λ‹ˆλ‹€. 그런 μš”μ†Œκ°€ μ—†λ‹€λ©΄ undefinedλ₯Ό λ°˜ν™˜ν•œλ‹€.

> ```javascript
const array1 = [5, 12, 8, 130, 44];
const found = array1.find(element => element > 10);
console.log(found);
// expected output: 12
JP
μ΄μ€‘ν‘œFrontend Engineer

3λ…„μ°¨ ν”„λ‘ νŠΈμ—”λ“œ 개발자. Next.js, React, TypeScript 기반 μ›Ή μ• ν”Œλ¦¬μΌ€μ΄μ…˜ 개발 μ „λ¬Έ. λŒ€κ·œλͺ¨ νŠΈλž˜ν”½ ν™˜κ²½μ—μ„œ SSRΒ·ISR λ Œλ”λ§ μ „λž΅ 섀계 κ²½ν—˜.

이λ ₯μ„œ 보기→