如何对JSON变量进行循环,并按照af变量的大小对它们进行排序,并保留其他标记

时间:2022-12-17 07:24:47

I need to loop through my JSON file, where info is stored in user id's, like this.

我需要循环遍历JSON文件,其中的信息存储在用户id中,如下所示。

{
  "350707981178109964":{"wins":1,"losses":0,"rank":4,"username":"TheeSniper95"},
  "426459326031593482":{"wins":0,"losses":0,"rank":1,"username":"Ding Dang Test"},
  "267752826623492102":{"wins":0,"losses":0,"rank":1,"username":"MrDooba"}
}

The long number is the user id accessed through member.id or message.author.id using the discord.js package.

长号是通过成员访问的用户id。id或message.author。使用不和id。js包。

I need to grab the user and put them at the top of an array with their user names and wins, the higher their wins the higher they are on the leaderboard. But I have had trouble keeping the username with the wins. And getting the user id to access it and sort through all of them and then save it in a variable.

我需要抓取用户并把他们放在一个数组的顶部,上面有他们的用户名和赢,他们赢的越高,他们在排行榜上的位置就越高。但是我在保存用户名时遇到了麻烦。让用户id访问它并对它们进行排序然后将其保存到一个变量中。

3 个解决方案

#1


1  

If you just need an array of users without their ids sorted by wins, you can try this:

如果您只需要一个用户数组,而不按wins排序,您可以尝试:

let board = Object.values(users).sort((a, b) => b.wins - a.wins);

The Object.values method returns an array of object's values (which are your users) and then you sort them by wins in descending order using sort function.

对象。值方法返回一个对象的值数组(它们是您的用户),然后使用sort函数按wins的降序排序。

If you need to include user id in your objects, you can use Object.entries to get all object's [key, value] pairs and them use the map function to create an array of users with their ids included:

如果需要在对象中包含用户id,可以使用Object。获取所有对象的[key, value]对的条目,并使用map函数创建包含其id的用户数组:

let users = {
  "350707981178109964":{"wins":1,"losses":0,"rank":4,"username":"TheeSniper95"},
  "426459326031593482":{"wins":0,"losses":1,"rank":1,"username":"Ding Dang Test"},
  "267752826623492102":{"wins":10,"losses":0,"rank":1,"username":"MrDooba"},
  "267752827723492576":{"wins":3,"losses":0,"rank":1,"username":"Johny"},
  "267733277234925765":{"wins":7,"losses":4,"rank":1,"username":"Sam"}
};

let board = Object.entries(users)
              .map(([key, val]) => ({id: key, ...val}))
              .sort((a, b) => b.wins - a.wins);

console.log(board);

#2


1  

var res = [];
var obj = {
  "350707981178109964": {"wins":1,"losses":0,"rank":4,"username":"TheeSniper95"},
  "42645932603159342": {"wins":0,"losses":0,"rank":1,"username":"Ding Dang Test"},
  "267752826623492102": {"wins":0,"losses":0,"rank":1,"username":"MrDooba"}
}
var  users = Object.keys(obj)
users.sort(function(a,b) {
  return obj[a].wins - obj[b].wins;
})
users.forEach(function(user) {
  var newObj = {
    user: user,
    details: obj[user]
  }
  res.push(newObj);
})

#3


0  

to sort a collection you can use underscore.js

要对集合进行排序,可以使用underscore.js

var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}];
_.sortBy(stooges, 'name');
=> [{name: 'curly', age: 60}, {name: 'larry', age: 50}, {name: 'moe', age: 40}];

#1


1  

If you just need an array of users without their ids sorted by wins, you can try this:

如果您只需要一个用户数组,而不按wins排序,您可以尝试:

let board = Object.values(users).sort((a, b) => b.wins - a.wins);

The Object.values method returns an array of object's values (which are your users) and then you sort them by wins in descending order using sort function.

对象。值方法返回一个对象的值数组(它们是您的用户),然后使用sort函数按wins的降序排序。

If you need to include user id in your objects, you can use Object.entries to get all object's [key, value] pairs and them use the map function to create an array of users with their ids included:

如果需要在对象中包含用户id,可以使用Object。获取所有对象的[key, value]对的条目,并使用map函数创建包含其id的用户数组:

let users = {
  "350707981178109964":{"wins":1,"losses":0,"rank":4,"username":"TheeSniper95"},
  "426459326031593482":{"wins":0,"losses":1,"rank":1,"username":"Ding Dang Test"},
  "267752826623492102":{"wins":10,"losses":0,"rank":1,"username":"MrDooba"},
  "267752827723492576":{"wins":3,"losses":0,"rank":1,"username":"Johny"},
  "267733277234925765":{"wins":7,"losses":4,"rank":1,"username":"Sam"}
};

let board = Object.entries(users)
              .map(([key, val]) => ({id: key, ...val}))
              .sort((a, b) => b.wins - a.wins);

console.log(board);

#2


1  

var res = [];
var obj = {
  "350707981178109964": {"wins":1,"losses":0,"rank":4,"username":"TheeSniper95"},
  "42645932603159342": {"wins":0,"losses":0,"rank":1,"username":"Ding Dang Test"},
  "267752826623492102": {"wins":0,"losses":0,"rank":1,"username":"MrDooba"}
}
var  users = Object.keys(obj)
users.sort(function(a,b) {
  return obj[a].wins - obj[b].wins;
})
users.forEach(function(user) {
  var newObj = {
    user: user,
    details: obj[user]
  }
  res.push(newObj);
})

#3


0  

to sort a collection you can use underscore.js

要对集合进行排序,可以使用underscore.js

var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}];
_.sortBy(stooges, 'name');
=> [{name: 'curly', age: 60}, {name: 'larry', age: 50}, {name: 'moe', age: 40}];