'Efficient way to filter an array | Discord.js v13
So I am trying to make a leaderboard command specific to the server the command is run in, however I have found that it is very inefficient and takes a very long time.
Here is my code below:
let m = await msg.channel.send("Loading... :loading:");
let top = Object.entries(lb).sort((a, b) => a[1] - b[1]).reverse();
let count = 0, topu = 0, tope = [];
while (count < 3) {
if (topu > (top.length - 1)) {
while (count < 3) {
tope.push("No user found");
count++;
};
break;
};
try {
await msg.guild.members.fetch(top[topu][0]);
} catch {
topu++;
continue;
};
tope.push(`<@${top[topu][0]}> - ${top[topu][1]} points`);
count++;
topu++;
};
let e = new Discord.MessageEmbed().setDescription(`🥇 ${tope[0]}\n\n🥈 ${tope[1]}\n\n🥉 ${tope[2]}`);
m.edit({ content: `Top 3 players in **${msg.guild.name}**`, embeds: [e] });
Does anyone happen to know a better algorithm to do something like this?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
