简体   繁体   English

按递减值对JS对象排序(使用未知键)

[英]Sort JS object by decrementing value (with unknown key)

I'm trying to sort a simple object with unknown keys by highest integer value. 我试图按最高整数值对具有未知键的简单对象进行排序。 Is there neat code to do this? 是否有简洁的代码可以做到这一点?

I've tried this, but with no success. 我已经尝试过了,但是没有成功。

let fixSort = {};
  Object.keys(leaderboard).sort().forEach(function(key){
  fixSort[key] = leaderboard[key];
});
console.log(fixSort);

Here is the object: 这是对象:

let leaderboard = {
  "Bob" : 2,
  "Jimmy" : 1,
  "Emma" : 6
}

My goal is to have it sorted as: 我的目标是将其排序为:

{
  "Emma" : 6,
  "Bob" : 2,
  "Jimmy" : 1
}

Well "you could": 好吧,“你可以”:

 const fixSort = {};

 for(const [k, v] of Object.entries(leaderboard).sort((a, b) => a[1] - b[1]))
  fixSort[k] = v;
 }

But thats honestly a bad way to structure this (as adding new key-value pairs is impossible as you have to recreate the whole thing). 但是,老实说,这是一种构造此方法的坏方法(因为添加新的键值对是不可能的,因为您必须重新创建整个对象)。 Instead use an array of objects: 而是使用对象数组:

  const leaderboard = [
   { name: "Jack", score: 7 },
   //...
 ];

If you need a lookuptable, then just create one based on that object: 如果需要查找表,则只需基于该对象创建一个查找表:

 const byName = {};
 for(const player of leaderboard)
   byName[player.name] = player;

For sure you can also sort the array: 当然,您也可以对数组进行排序:

 leaderboard.sort((a, b) => a.score - b.score);

Then you can easily add / remove players. 然后,您可以轻松添加/删除播放器。

 function addToLeaderBoard(player) {
   leaderboard.push(player);
   leaderboard.sort((a, b) => a.score - b.score);
   byName[player.name] = player;
 }

You're close, but Array.prototype.sort can take a custom compare function. 您已经接近了,但是Array.prototype.sort可以接受自定义比较功能。 That's what you want. 那正是你想要的。

// Gives you an array
const keyArray = Object.keys(leaderboard)
const fixedSort = keyArray.sort((a, b) => {
  if (leaderboard[a] > leaderboard[b]) {
    return 1
  }
  return -1
})
console.log(fixedSort);
let leaderboard = {
  "Bob" : 2,
  "Jimmy" : 1,
  "Emma" : 6
}

var newObj={};

Object.keys(leaderboard).sort((a,b)=>leaderboard[b]-leaderboard[a]).forEach((i, v)=> newObj[i]=leaderboard[i]);

在此处输入图片说明

For change Object to sorted array, Because If you try to expand the obj in console it will show, you again unsorted, but in real it is sorted, For completely clear, our doubt, we can change this object to array use below code 对于将对象更改为已排序的数组,因为如果尝试在控制台中扩展obj,它将再次显示未排序的内容,但实际上它是已排序的,为了完全清楚,我们的疑问是,我们可以使用以下代码将此对象更改为数组

var result = Object.keys(newObj).map(function(key) {
  return [key, newObj[key]];
});
console.log(result);

it will look like below image 它看起来像下面的图像 在此处输入图片说明

Another option - use Object.entries to create an array from the leaderboard , sort it by value, then rebuild individual objects into an array, to maintain the sorted order. 另一个选择-使用Object.entriesleaderboard创建数组,按值对数组进行排序,然后将单个对象重建为数组,以保持排序顺序。

 const leaderboard = { "Bob" : 2, "Jimmy" : 1, "Emma" : 6 } console.log(Object.entries(leaderboard).sort((a,b) => b[1] - a[1]).map(person => ({[person[0]]: person[1]}))) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM