Skip to content

Commit

Permalink
fix-post: map-filter-reduce
Browse files Browse the repository at this point in the history
  • Loading branch information
june-by committed Dec 21, 2023
1 parent 0c1fb22 commit 68f03c8
Showing 1 changed file with 88 additions and 5 deletions.
93 changes: 88 additions & 5 deletions contents/posts/JavaScript/map-filter-reduce.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ thumbNail: "https://github.com/BY-juun/Blog/assets/78716842/78919fbe-9e62-4e2b-9

# 1๏ธโƒฃ map

`map()` ํ•จ์ˆ˜๋Š” ๋ฐฐ์—ด ๋‚ด์˜ ๋ชจ๋“  ์š”์†Œ ๊ฐ๊ฐ์— ๋Œ€ํ•ด call back ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ชจ์•„ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ ํ•ฉ๋‹ˆ๋‹ค.
`map` ํ•จ์ˆ˜์˜ ์›ํ˜•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
`map()` ํ•จ์ˆ˜๋Š” ๋ฐฐ์—ด ๋‚ด์˜ ๋ชจ๋“  ์š”์†Œ ๊ฐ๊ฐ์— ๋Œ€ํ•ด call back ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ชจ์•„ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ ํ•ฉ๋‹ˆ๋‹ค.

`map(value,index,arr)`
`map` ํ•จ์ˆ˜๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ํ˜•ํƒœ์˜ ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์ธ์ž๋กœ ๋ฐ›์Šต๋‹ˆ๋‹ค.

`(value,index,arr) => {}`

value๋Š” ๋ฐฐ์—ด์˜ ์š”์†Œ๋ฅผ ๋งํ•˜๊ณ , index๋Š” ํ•ด๋‹น ์š”์†Œ์˜ ์ธ๋ฑ์Šค, arr์€ ํ•ด๋‹น mapํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ ๋ฐฐ์—ด์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

```js showLineNumbers
```js
const array = [1, 4, 9, 16];

const map = array.map((x) => x * 2);
Expand All @@ -27,7 +28,7 @@ console.log(map); //[2, 8, 18, 32]

๋ฐฐ์—ด ๋‚ด์˜ ์š”์†Œ๊ฐ€ ๊ฐ์ฒด์ธ ๊ฒฝ์šฐ์—๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

```js showLineNumbers
```js
var kvArray = [
{ key: 1, value: 10 },
{ key: 2, value: 20 },
Expand Down Expand Up @@ -57,6 +58,38 @@ var reformattedArray = kvArray.map(function (obj) {

๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•จ์ˆ˜์•ˆ์—์„œ ํ™€์ˆ˜ ์ง์ˆ˜๋ฅผ ๋‚˜๋ˆ„์–ด ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

## ์ง์ ‘ ๊ตฌํ˜„ํ•ด๋ณด๊ธฐ

`map` ํ•จ์ˆ˜๋ฅผ ์ง์ ‘ ๊ตฌํ˜„ ํ›„ `Array.prototype`์— ๊ฐ์ฒด์— ๋“ฑ๋ก ํ›„ ์‚ฌ์šฉํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

```js
Array.prototype.myMap = function myMap(cb) {
const result = [];

for (let idx = 0; idx < this.length; idx++) {
result.push(cb(this[idx], idx, this));
}

return result;
};
```

```js
[1, 2, 3].myMap((v) => v * 2); // [2, 4, 6]
```

```js
[1, 2, 3].myMap((v, idx) => {
if (idx > 1) {
return v * 2;
} else {
return v;
}
}); // [1, 2, 6]
```

<br />

# 2๏ธโƒฃ filter

filterํ•จ์ˆ˜๋Š” ์ฃผ์–ด์ง„ ํ•จ์ˆ˜์˜ _*ํ…Œ์ŠคํŠธ๋ฅผ ํ†ต๊ณผํ•˜๋Š” ๋ชจ๋“  ์š”์†Œ๋ฅผ ๋ชจ์•„ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜*_ ํ•ฉ๋‹ˆ๋‹ค.
Expand Down Expand Up @@ -96,6 +129,32 @@ console.log(richNames); // ["YD", "Bill"]

filter์˜ ๋ฐ˜ํ™˜๊ฐ’์ด ๋ฐฐ์—ด์ด๋ผ๋Š” ์ ์„ ์ด์šฉํ•ด mapํ•จ์ˆ˜์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

## ์ง์ ‘ ๊ตฌํ˜„ํ•ด๋ณด๊ธฐ

`filter` ํ•จ์ˆ˜๋ฅผ ์ง์ ‘ ๊ตฌํ˜„ ํ›„ `Array.prototype`์— ๊ฐ์ฒด์— ๋“ฑ๋ก ํ›„ ์‚ฌ์šฉํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

```js
Array.prototype.myFilter = function myMap(cb) {
const result = [];

for (let idx = 0; idx < this.length; idx++) {
if (cb(this[idx], idx, this)) {
result.push(this[idx]);
}
}

return result;
};
```

```js
[1, 2, 3].myFilter((v) => v > 1); // [2, 3]
```

```js
[1, 2, 3].myFilter((v, idx) => idx > 1); // [3]
```

<br />

# 3๏ธโƒฃ reduce
Expand Down Expand Up @@ -144,3 +203,27 @@ const result = arr.reduce((acc, cur, i) => {

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ดˆ๊ธฐ๊ฐ’์„ ๋ฐฐ์—ด๋กœ ์„ธํŒ…ํ•˜๊ณ , ์กฐ๊ฑด๋ฌธ์„ ์ด์šฉํ•˜๋ฉด, filterํ•จ์ˆ˜์˜ ์—ญํ• ์„ ํ•˜๋Š” reduceํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
filterํ•จ์ˆ˜ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ, sort, find, findIndex ๋“ฑ๋“ฑ ๋‹ค๋ฅธ ํ•จ์ˆ˜๋“ค๋„ reduceํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

## ์ง์ ‘ ๊ตฌํ˜„ํ•ด๋ณด๊ธฐ

`reduce` ํ•จ์ˆ˜๋ฅผ ์ง์ ‘ ๊ตฌํ˜„ ํ›„ `Array.prototype`์— ๊ฐ์ฒด์— ๋“ฑ๋ก ํ›„ ์‚ฌ์šฉํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

```js
Array.prototype.myReduce = function myMap(cb, initialValue) {
let result = initialValue;

for (let idx = 0; idx < this.length; idx++) {
result = cb(result, this[idx], this);
}

return result;
};
```

```js
[1, 2, 3].myReduce((acc, cur) => acc + cur, 0); // 6
```

```js
["1", "2", "3"].myReduce((acc, cur) => acc + cur, ""); // "123"
```

0 comments on commit 68f03c8

Please sign in to comment.