<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>h_chulho.log</title>
        <link>https://velog.io/</link>
        <description></description>
        <lastBuildDate>Sat, 06 Jan 2024 18:00:07 GMT</lastBuildDate>
        <docs>https://validator.w3.org/feed/docs/rss2.html</docs>
        <generator>https://github.com/jpmonette/feed</generator>
        <image>
            <title>h_chulho.log</title>
            <url>https://velog.velcdn.com/images/h_chulho/profile/9dabd771-cf43-4dad-b6e5-411408cfb7e9/image.jpg</url>
            <link>https://velog.io/</link>
        </image>
        <copyright>Copyright (C) 2019. h_chulho.log. All rights reserved.</copyright>
        <atom:link href="https://v2.velog.io/rss/h_chulho" rel="self" type="application/rss+xml"/>
        <item>
            <title><![CDATA[[Javascript] Array.some 과 화살표함수 사용시 {},return생략 여부 확인]]></title>
            <link>https://velog.io/@h_chulho/Javascript-Array.some-%EA%B3%BC-%ED%99%94%EC%82%B4%ED%91%9C%ED%95%A8%EC%88%98-%EC%82%AC%EC%9A%A9%EC%8B%9C-return%EC%83%9D%EB%9E%B5-%EC%97%AC%EB%B6%80-%ED%99%95%EC%9D%B8</link>
            <guid>https://velog.io/@h_chulho/Javascript-Array.some-%EA%B3%BC-%ED%99%94%EC%82%B4%ED%91%9C%ED%95%A8%EC%88%98-%EC%82%AC%EC%9A%A9%EC%8B%9C-return%EC%83%9D%EB%9E%B5-%EC%97%AC%EB%B6%80-%ED%99%95%EC%9D%B8</guid>
            <pubDate>Sat, 06 Jan 2024 18:00:07 GMT</pubDate>
            <description><![CDATA[<h1 id="arraysome">Array.some</h1>
<p>쇼핑리스트에 새로운 품목을 추가했는데, 기존 리스트에 있는 품목임에도 새로운 리스트로 추가됨 -&gt; 이름이 같은 경우 기존 리스트의 수량 변경으로 수정.</p>
<h3 id="기존-코드">기존 코드</h3>
<pre><code class="language-javascript">
  const addList = (newItem) =&gt; {
      setLists((currItem) =&gt; {
       return [...currItem, { ...newItem, id: uuid() }];
     });
   };</code></pre>
<p>기존에 알고있던 map, filter로 할 수 있을지 고민하다, boolean 값을 반환하는 some 메소드를 사용해보기로 했다.
<br></p>
<blockquote>
<p>Array.some 은 배열 내 각각의 요소를 순회하면서 true/false 여부를 확인하고, 적어도 하나의 true값이 나오는 즉시 배열 순회를 멈춘다. </p>
</blockquote>
<p>_true 가 나오면 즉시 멈춘다는 점에서 map이나 filter보다 빠르지 않을까?_🤔</p>
<h3 id="수정-코드">수정 코드</h3>
<pre><code class="language-javascript">const addList = (newItem) =&gt; {
    const isSameProduct = lists.some((currItem) =&gt; {
      currItem.product === newItem.product;
    });
    if (isSameProduct) {
      lists.map((currItem) =&gt; {
        if (currItem.product === newItem.product) {
          currItem.quantity += newItem.quantity;
          setLists((currItem) =&gt; {
            return [...currItem];
          });
        }
      });
    } else {
      setLists((currItem) =&gt; {
        return [...currItem, { ...newItem, id: uuid() }];
      });
    }
  };</code></pre>
<p>먼저 Array.some 메소드의 결과값을 isSameProduct 변수로 받고, Boolean결과에 따라 함수를 실행한다. 
<br>
분명 맞게 쓴 것 같은데...같은 품목을 추가해도 isSameProduct의 결과값이 console.log으로 확인해보아도 계속 false만 나온다.</p>
<h1 id="화살표함수-주의사항">화살표함수 주의사항</h1>
<pre><code class="language-javascript">const isSameProduct = lists.some((currItem) =&gt; {
      return currItem.product === newItem.product;
    });</code></pre>
<p><br>화살표함수의 경우 <a href="https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Functions/Arrow_functions">중괄호와 &quot;return&quot;을 제거하면 반환이 암시된다.</a> 반면 습관적으로 중괄호로 함수를 작성하고, 화살표함수니까 return을 안적어도되지? 하면서 생략했었던 것😂</p>
<p>따라서 위 코드처럼 중괄호를 사용해 return값을 반환하고자 한다면 return을 반드시 적어야 한다.</p>
<h2 id="쇼핑리스트-review">쇼핑리스트 review</h2>
<p><input type="checkbox" checked> 추가된 리스트 수량변경 
<input type="checkbox" checked> 개별삭제, 전체Reset 버튼
<input type="checkbox" checked> Local Storage 사용, useEffect</p>
]]></description>
        </item>
    </channel>
</rss>