-3

I'mm making discord.js bot and i want make command which will write 5 users with top balance. When i runned this code

let coins = require("./coins.json");
for (var key in coins) {
  if (coins.hasOwnProperty(key)) {
  console.log(key + ": " + coins[key].coins);
  }
}

I got returned all the strings...

298462251453775873: 2090

208120625947082752: 210

461643208422588446: 370

290099182310785025: 10

468737161563209738: 420

381182620513468417: 40

408908116101431298: 180...

So means: User: coins. How can i sort those values without losing which user owns it?

Tomáš Kordoš
  • 329
  • 2
  • 12
  • Not by key values, but by key.coins values – Tomáš Kordoš Nov 27 '18 at 17:59
  • Important to understand difference between an array and an object literal. Code shown does not appear to be array related and objects themselves can't be sorted but can be converted to sortable arrays – charlietfl Nov 27 '18 at 18:05

3 Answers3

1

Using Object.keys() and array.slice(), you can achieve what you need.

const coins = {
  "298462251453775873": { coins: 2090 },
  "208120625947082752": { coins: 210 },
  "461643208422588446": { coins: 370 },
  "290099182310785025": { coins: 10 },
  "468737161563209738": { coins: 420 },
  "381182620513468417": { coins: 40 },
  "408908116101431298": { coins: 180 }
}

const top5Coins = Object.keys(coins).sort(function(a,b){return coins[a].coins-coins[b].coins}).slice(0,5);

const mappedTop5 = top5Coins.map(c => `${c}:${coins[c].coins}`);

console.log(mappedTop5.join("\n"));
Jonathan Hamel
  • 1,393
  • 13
  • 18
0

So convert it to an array with Object.keys or Object.entries and sort that.

const users = {
  '1': 100,
  '2': 200,
  '3': 50
}

const sorted = Object.entries(users).sort((a, b) => a[1] > b[1] ? -1 : 1)
console.log(sorted)

const users = {
  '1': 100,
  '2': 200,
  '3': 50
}

const sorted = Object.keys(users).sort((a, b) => users[a] > users[b] ? -1 : 1)
console.log(sorted)

sorted.forEach(k => {
  console.log(k, users[k])
})
epascarello
  • 204,599
  • 20
  • 195
  • 236
  • Yes, this returns me More Arrays. how can i get value 1 from array 1? – Tomáš Kordoš Nov 27 '18 at 18:07
  • So you read index 0 for the id and 1 for the value.... and the second one shows you how to get the order and reference the value.... both answer the question. There is no way to order the object. – epascarello Nov 27 '18 at 18:13
0

You can use Object.entries, Array.map like below to achieve sorting for objects

let coins = {
    '298462251453775873': {coins: 2090}
  , '208120625947082752': {coins: 210}
  , '461643208422588446': {coins: 370}
  , '290099182310785025': {coins: 10}
  , '468737161563209738': {coins: 420}
  , '381182620513468417': {coins: 40}
  , '408908116101431298': {coins: 180}
}

coins = Object.assign(...Object.entries(coins)
                               .sort(([,a],[,b]) => a.coins - b.coins)
                               .map(([k,v]) => ({[k]: v})))

for (var key in coins) {
  if (coins.hasOwnProperty(key)) {
  console.log(key + ": " + coins[key].coins);
  }
}

//console.log(coins)
Nitish Narang
  • 4,124
  • 2
  • 15
  • 22