Home > Back-end >  Javascript Discord: Sort object of object and get top 3
Javascript Discord: Sort object of object and get top 3

Time:09-17

I am trying to sort a discord bot to display the top 3 members. However I keep getting the error:

JavaScript error: Uncaught TypeError: guildStats.slice

This is what I tried:

var guildStats = {
  '1234567': {
    xp: 95,
    level: 0,
    last_message: 1631181685724,
    invited: {},
    invited_by: 0
  },
  '0987654': {
    xp: 13,
    level: 0,
    last_message: 1631181527799,
    invited: {},
    invited_by: 0
  },
  '243562345': {
    xp: 18,
    level: 0,
    last_message: 1631181537020,
    invited: {},
    invited_by: 0
  },
  '76533465': {
    xp: 14,
    level: 0,
    last_message: 1631181536875,
    invited: {},
    invited_by: 0
  },
  '34667634567': {
    xp: 8,
    level: 0,
    last_message: 1631181659142,
    invited: {},
    invited_by: 0
  },
  '346534': {
    xp: 98,
    level: 0,
    last_message: 1631181638743,
    invited: {},
    invited_by: 0
  },
  '34343677677886': {
    xp: -63,
    level: 0,
    last_message: 1631181584314,
    invited: {},
    invited_by: 0
  },
  '987654345676543': {
    xp: 20,
    level: 0,
    last_message: 1631181589153,
    invited: {},
    invited_by: 0
  },
  '75634576786588': {
    xp: -140,
    level: 0,
    last_message: 1631181593304,
    invited: {},
    invited_by: 0
  },
  '34343434556566': {
    xp: 43,
    level: 0,
    last_message: 1631181663340,
    invited: {},
    invited_by: 0
  }
};


var byXP = guildStats.slice(0);
byXP.sort(function(a,b) {
    return a.xp - b.xp;
});

document.write(byXP);

What I want to output is

The top 3 members by XP are: 
1. 346534
2. 1234567
3. 34343434556566

I know i've messed up somewhere.

How do I sort all the objects in guildStats by the child objects key xp's value

Thanks for the help.

CodePudding user response:

You can use Object.fromEntries() to turn your object into an array of [key,value] pairs.

We can then sort on xp and return the top three entries using Array.slice().

var guildStats = { '1234567': { xp: 95, level: 0, last_message: 1631181685724, invited: {}, invited_by: 0 }, '0987654': { xp: 13, level: 0, last_message: 1631181527799, invited: {}, invited_by: 0 }, '243562345': { xp: 18, level: 0, last_message: 1631181537020, invited: {}, invited_by: 0 }, '76533465': { xp: 14, level: 0, last_message: 1631181536875, invited: {}, invited_by: 0 }, '34667634567': { xp: 8, level: 0, last_message: 1631181659142, invited: {}, invited_by: 0 }, '346534': { xp: 98, level: 0, last_message: 1631181638743, invited: {}, invited_by: 0 }, '34343677677886': { xp: -63, level: 0, last_message: 1631181584314, invited: {}, invited_by: 0 }, '987654345676543': { xp: 20, level: 0, last_message: 1631181589153, invited: {}, invited_by: 0 }, '75634576786588': { xp: -140, level: 0, last_message: 1631181593304, invited: {}, invited_by: 0 }, '34343434556566': { xp: 43, level: 0, last_message: 1631181663340, invited: {}, invited_by: 0 } }; 

var byXP = Object.entries(guildStats);
byXP.sort(function([keya,valuea],[keyb,valueb]) {
    return valueb.xp - valuea.xp;
});

console.log('The top 3 members by XP are:');
byXP.slice(0,3).forEach(([key, member],idx) => console.log(`${idx 1}. ${key}, xp: ${member.xp}`));

  • Related