header-img
Info :
728x90

1. filter : λ°°μ—΄ λ©”μ„œλ“œ

- λͺ¨λ“  λ°°μ—΄μ˜ μš”μ†Œ μ€‘μ—μ„œ νŠΉμ • 쑰건을 λ§Œμ‘±ν•˜λŠ” μš”μ†Œλ₯Ό κ±ΈλŸ¬λ‚΄λŠ” λ©”μ„œλ“œ

- filter λ©”μ„œλ“œ μ‚¬μš© μ˜ˆμ‹œ

// ν•¨μˆ˜ ν‘œν˜„μ‹
const isEven = function (num) {
	return num%2 === 0;
}

let arr = [1,2,3,4];
// '짝수'λ₯Ό νŒλ³„ν•˜λŠ” ν•¨μˆ˜κ°€ μ‘°κ±΄μœΌλ‘œμ„œ filter λ©”μ„œλ“œμ˜ μ „λ‹¬μΈμžλ‘œ μ „λ‹¬λœλ‹€.
let output = arr.filter(isEven);
console.log(output); // ->> [2,4]

const isLteFive = function (str) {
	// Lte = less than equal
    return str.length <= 5;
};

arr = ['hello', 'code', 'states', 'happy', 'hacking'];
// '길이 5 μ΄ν•˜'λ₯Ό νŒλ³„ν•˜λŠ” ν•¨μˆ˜κ°€ μ‘°κ±΄μœΌλ‘œμ„œ filter λ©”μ„œλ“œμ˜ μ „λ‹¬μΈμžλ‘œ μ „λ‹¬λœλ‹€.
let output = arr.filter(isLteFive);
console.log(output); // ->> ['hello', 'code', 'happy']

- filter ν™œμš© μ˜ˆμ‹œ

   1. λ°°μ—΄μ˜ 각 μš”μ†Œκ°€

   2. νŠΉμ • 논리(ν•¨μˆ˜)에 λ”°λ₯΄λ©΄, 사싀(true)일 λ•Œ

   3. λ”°λ‘œ λΆ„λ₯˜ν•©λ‹ˆλ‹€(filter).

2. Map

- filter ν™œμš© μ˜ˆμ‹œ

   1. λ°°μ—΄μ˜ 각 μš”μ†Œκ°€

   2. νŠΉμ • 논리(ν•¨μˆ˜)에 μ˜ν•΄

   3. λ‹€λ₯Έ μš”μ†Œλ‘œ 지정(map) λ©λ‹ˆλ‹€.

- ν•˜λ‚˜μ˜ 데이터λ₯Ό λ‹€λ₯Έ λ°μ΄ν„°λ‘œ 맀핑(mapping) ν•  λ•Œ μ‚¬μš©ν•©λ‹ˆλ‹€.

3. Reduce

- Reduce ν™œμš© μ˜ˆμ‹œ

   1. λ°°μ—΄μ˜ 각 μš”μ†Œλ₯Ό

   2. νŠΉμ • 방법(ν•¨μˆ˜)에 따라

   3. μ›ν•˜λŠ” ν•˜λ‚˜μ˜ ν˜•νƒœλ‘œ

   4. μ‘μΆ•ν•©λ‹ˆλ‹€. (reduction)

728x90
더보기
Document/λ¨•μ„ μƒμ˜ 코딩ꡐ싀