JavaScript

[JavaScript] filter 와 push 의 return 값

이경욱 2024. 1. 24. 19:47
  const onClickLikeHandler = () => {
    if (target.length) {
      const likeCounter = postlike - 1;
      console.log(postInfo);
      const postInfoData = postInfo.filter(e => e.id !== userInfo.id);
      const param = {id: postId, likeUserInfo: postInfoData, likeCount: likeCounter};
      likeMutation.mutate(param);
      setLiked(false);
    } else {
      const likeCounter = postlike + 1;
      postInfo.push(userInfo);
      const param = {id: postId, likeUserInfo: postInfo, likeCount: likeCounter};
      likeMutation.mutate(param);
      setLiked(true);
    }
  };

 

filter 는 변수선언을 해줘야 retrun 값을 받아와서 할당할 수 있다 .

반면에 push는 retrun 값을 length로 해주기 때문에 기존의 배열을 참조해서 가져가야 

배열에 추가된 값을 사용할 수 있다.

 

 

 

 

'JavaScript' 카테고리의 다른 글

preventDefault  (1) 2023.10.16
deltaY  (0) 2023.10.13
addEventListener  (0) 2023.10.13