<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>panxoat08_.log</title>
        <link>https://velog.io/</link>
        <description>Front-end Developer</description>
        <lastBuildDate>Wed, 22 Nov 2023 08:12:32 GMT</lastBuildDate>
        <docs>https://validator.w3.org/feed/docs/rss2.html</docs>
        <generator>https://github.com/jpmonette/feed</generator>
        <image>
            <title>panxoat08_.log</title>
            <url>https://images.velog.io/images/panxoat08_/profile/0db87aa5-3436-4cda-abe3-cd207a39e29f/social.jpeg</url>
            <link>https://velog.io/</link>
        </image>
        <copyright>Copyright (C) 2019. panxoat08_.log. All rights reserved.</copyright>
        <atom:link href="https://v2.velog.io/rss/panxoat08_" rel="self" type="application/rss+xml"/>
        <item>
            <title><![CDATA[Javascript 고차함수 reduce()에 대해 알아보자]]></title>
            <link>https://velog.io/@panxoat08_/Javascript-%EA%B3%A0%EC%B0%A8%ED%95%A8%EC%88%98-reduce%EC%97%90-%EB%8C%80%ED%95%B4-%EC%95%8C%EC%95%84%EB%B3%B4%EC%9E%90</link>
            <guid>https://velog.io/@panxoat08_/Javascript-%EA%B3%A0%EC%B0%A8%ED%95%A8%EC%88%98-reduce%EC%97%90-%EB%8C%80%ED%95%B4-%EC%95%8C%EC%95%84%EB%B3%B4%EC%9E%90</guid>
            <pubDate>Wed, 22 Nov 2023 08:12:32 GMT</pubDate>
            <description><![CDATA[<p>저는 고차함수를 좋아합니다.</p>
<p>map, reduce를 애용하는데요.
이것만 있다면 어떤 데이터를 제 앞에 던져줘도 전처리가 가능합니다.</p>
<h4 id="오늘은-reduce는-어떤-친구인지-알아보겠습니다">오늘은 reduce()는 어떤 친구인지 알아보겠습니다.</h4>
<hr>
<h3 id="demo-arrayprototypereduce">Demo: Array.prototype.reduce</h3>
<pre><code class="language-javascript">const array1 = [1, 2, 3, 4];

// 0 + 1 + 2 + 3 + 4
const initialValue = 0;

const sumWithInitial = array1.reduce(
  (accumulator, currentValue) =&gt; accumulator + currentValue,
  initialValue,
);

console.log(sumWithInitial);
// Expected output: 10</code></pre>
<p>발췌 : <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce">MDN</a></p>
<hr>
<h3 id="내부적으로-어떤-동작을-하고-있을까">내부적으로 어떤 동작을 하고 있을까?</h3>
<p>공식문서를 확인한다면 더할나위 없이 좋습니다.
하지만 모든 책에는 목차가 있듯, 대주제를 훑고 들어간다면 더 빠른 이해가 가능합니다.</p>
<h4 id="demo코드를-보시면-짐작가능한-동작들이-있습니다">Demo코드를 보시면 짐작가능한 동작들이 있습니다.</h4>
<blockquote>
<ol>
<li>3번째 줄 주석이 뭘 말하고 싶은거지? 아, array1의 원소들을 모두 더하는 작업이란걸 설명하고 있구나.</li>
<li>변수 이름을 accumulator라고 사용했네? accmulator의 뜻이 뭐지? 아, 누산기?</li>
<li>아, reduce()는 자신을 호출한 배열의 원소들을 돌면서 누산해주는 역할이구나</li>
</ol>
</blockquote>
<h4 id="진짜-대충-reduce가-어떻게-동작하는지-알았으니-실제로-그런지-확인해봅시다">진짜 대충 reduce()가 어떻게 동작하는지 알았으니 실제로 그런지 확인해봅시다.</h4>
<pre><code class="language-typescript">arr.reduce(callbackFn, initialValue)</code></pre>
<p>reduce는 크게 보면 2개의 인자를 받습니다.</p>
<pre><code class="language-typescript">1. callbackFn: 함수
2. initialValue?: 변수 // 전달하지 않아도 됨</code></pre>
<p>다시, callbackFn(함수)을 작게 보면 4개의 인자를 받습니다.</p>
<pre><code>1. accumulator
2. currentValue
3. currentIndex
4. array</code></pre><p>4개의 인자는 각각 어떤 변수인지 간단하게 알아봅시다.</p>
<ol>
<li>accumulator<ul>
<li>callbackFn 함수의 return값이 누적될 변수 (반환될 값)</li>
<li>initialValue를 전달할 경우 initialValue의 값이 곧 accumulator의 초깃값, </li>
<li>initialValue를 전달하지 않을 경우 reduce()를 호출한 배열의 0번째 원소가 곧 accumulator의 초깃값</li>
</ul>
</li>
<li>currentValue<ul>
<li>reduce()를 호출한 배열의 원소들이 차례로 들어올 변수</li>
<li>다른 예시로, for (const property in object)문의 &quot;property&quot;를 담당</li>
</ul>
</li>
<li>currentIndex<ul>
<li>현재 계산하고 있는 배열의 index를 나타낼 변수</li>
<li>initialValue를 전달하지 않을 경우 1부터 시작, 전달할 경우 0부터 시작</li>
</ul>
</li>
<li>array<ul>
<li>reduce()를 호출한 배열</li>
</ul>
</li>
</ol>
<hr>
<h3 id="어-변수들이-하는-역할들을-나열해보니-진짜-내가-생각했던것처럼-동작하는구나">어? 변수들이 하는 역할들을 나열해보니 진짜 내가 생각했던것처럼 동작하는구나?</h3>
<p>저는 조금 더 쉽게 설명하고 싶은데요,
위에 적어두었던 Demo: Array.prototype.reduce의 예제코드를 살짝 바꿔 보겠습니다.
코드의 의미는 동일합니다.</p>
<h4 id="더-간단하게-consolelog를-사용하여-알아봅시다">더 간단하게 console.log()를 사용하여 알아봅시다.</h4>
<pre><code class="language-typescript">const array1 = [1, 2, 3, 4];

const initialValue = 0;

function 덧셈을해주는_함수(accmulator, currentValue) {
  return accmulator + currentValue
}

const sumWithInitial = array1.reduce(
  (accumulator, currentValue, index) =&gt; {
      console.log(&quot;accumulator&quot;, accumulator)
      console.log(&quot;currentValue&quot;, currentValue)
      console.log(&quot;index&quot;, index)

      return 덧셈을해주는_함수(accumulator, currentValue);
  }, initialValue
);

// console //
accumulator 0
currentValue 1
index 0
덧셈을해주는_함수(accumulator, currentValue) 실행

accumulator 1 (0+1)
currentValue 2
index 1
덧셈을해주는_함수(...) 실행

accumulator 3 (1+2)
currentValue 3
index 2
덧셈을해주는_함수(...) 실행

accumulator 6 (3+3)
currentValue 4
index 3
return 덧셈을해주는_함수(6+4) // 10 (순환 종료)</code></pre>
<blockquote>
<p>💡 아, accumulator는 그냥 덧셈을해주는_함수()의 반환값을 계속 저장하고 있는 저장소구나</p>
</blockquote>
<hr>
<h3 id="응용-문제">응용 문제</h3>
<pre><code class="language-typescript">const obj = [{name: &quot;동동&quot;, &quot;age&quot;: 22}, {name: &quot;아리&quot;, &quot;age&quot;: &quot;26&quot;}];

const result = obj.reduce((accmulator, currentValue) =&gt; {
  const { name, age } = currentValue;

  if (age &gt; 23) {
    return [...accmulator, currentValue]
  }
  return accmulator;
}, [])

console.log(result) // ?</code></pre>
<p>result의 값은 무엇일까요??</p>
<hr>
<h3 id="마치며">마치며</h3>
<p>저는 reduce 함수를 무척 어려워했습니다.</p>
<p>아무리 공식 문서를 읽어도 그때뿐이고 왜 이렇게 동작하는지도 모른 채 막 사용했었던 때가 있었는데
이번이 저 자신도 한 번 더 공부하는 기회였고,</p>
<p>만약 JS에 처음 입문하시거나 reduce 함수가 익숙하지 않은 분들이 본 글을 읽었을 때 조금이나마
도움을 받았으면 좋겠습니다.</p>
<p>쉽게 설명하는 건 정말 어려운 것임을 다시 한번 깨닫습니다..</p>
<p>더 쉽게 이해할 수 있는 노하우가 있다면 알려주세요!
다음 글에서 담아보겠습니다</p>
<p>다음 글은</p>
<h4 id="javascript-고차함수-reduce를-직접-구현해-보자">Javascript 고차함수 reduce()를 직접 구현해 보자</h4>
<p>를 적어보고자 합니다.</p>
]]></description>
        </item>
    </channel>
</rss>