기능정리 - 해쉬태그 인기순으로 나열하기

이재근·2022년 8월 18일
0

const allRoom = await Rooms.findAll({ order: [["createdAt", "DESC"]] });

let tags = [];
for (let i = 0; i < allRoom.length; i++) {
  const room = allRoom[i];
  for (let l = 0; l < room.hashTag.length; l++) {
    const hashtag = room.hashTag[l];
    tags.push(hashtag);
  }
}

tags = tags.reduce((accu, curr) => {
  accu[curr] = (accu[curr] || 0) + 1;
  return accu;
}, {});
let max = 0;
let max2 = 0;
let max3 = 0;
for (let j = 0; j < Object.values(tags).length; j++) {
  if (max < Object.values(tags)[j]) {
    max = Object.values(tags)[j];
  }
  if (max2 < Object.values(tags)[j] < max) {
    max2 = Object.values(tags)[j];
  }
  if (max3 < Object.values(tags)[j] < max2) {
    max3 = Object.values(tags)[j];
  }
}
max = Object.keys(tags).find((key) => tags[key] === max);
delete tags[max];
max2 = Object.keys(tags).find((key) => tags[key] === max2);
delete tags[max2];
max3 = Object.keys(tags).find((key) => tags[key] === max3);
tags = [max, max2, max3];
profile
하루 고생하면 코드가 나 대신 일해준다.

0개의 댓글