⛵ 항해99/TIL · WIL ✏️

[TIL] 2023.10.23 - 따로 가져온 API 하나로 합쳐서 정렬하기

hhhhy 2023. 10. 24. 00:07

 따로 가져온 API 하나로 합쳐서 정렬하기 

 

① get 으로 가져온 2개의 데이터 각각 useState에 저장하기!

const [myCommentsList, setMyCommentsList] = useState([]); // get으로 가져온 사용자별 댓글 조회
const [myRepliesList, setMyRepliesList] = useState([]); // get으로 가져온 사용자별 대댓글 조회

 

② 하나로 합치기

const myCommentsRepliesList = [...myCommentsList, ...myRepliesList]; // 사용자별 댓글 + 대댓글

 

③ 정렬하기(내림차순)

const sortedLists = myCommentsRepliesList.sort(
    (a, b) => new Date(b.createAt) - new Date(a.createAt)
  );