简体   繁体   English

Javascript:对对象中的对象进行排序

[英]Javascript: sorting objects in objects

As you may assume - I'm stuck ;) My problem is that I have an object with some other objects within: 如您所料-我被卡住了;)我的问题是我有一个对象,其中包含一些其他对象:

let players = {
  buggy: {
    name: 'John',
    surname: 'Cocroach',
    rank: 872
  },
  valentino: {
    name: 'Antonio',
    surname: 'Valencia',
    rank: 788
  },
  tommy: {
    name: 'Tom',
    surname: 'Witcher',
    rank: 101
  },
};

And what I want to do is to sort "players" object by "rank": (tommy(101), valentino(788), buggy(872)) or by a string (eg "surname"). 我想做的是按“等级”对“玩家”对象进行排序:(tommy(101),valentino(788),buggy(872))或字符串(例如“姓”)。 What is more I want it to still be an object (I am using it in several other functions ;) ) 更重要的是,我希望它仍然是一个对象(我正在其他几个函数中使用它;))

I tried some ideas form here (eg converting to an array), but all were uneffective. 我在这里尝试了一些想法形式(例如转换为数组),但是都无效。 What would be the best option? 最好的选择是什么?

As objects are stored by reference you can have an array and an object operating on the same underlying player objects: 由于对象是通过引用存储的,因此您可以拥有一个数组和一个对相同的基础播放器对象进行操作的对象:

 const playersByRank = Object.values(players).sort((a, b) => a.rank - b.rank);

Now you can access a player either via its name under players or via its relative rank under playersByRank , eg taking the player "buggy" is the same as taking the highest ranked player: 现在,您可以通过在players下方的名称或在playersByRank下的相对排名访问玩家,例如,将玩家“ buggy” playersByRank排名最高的玩家相同:

players.buggy.test = "works";
console.log(playersByRank[0].test);

You can sort the keys and then return an iterable object. 您可以对键进行排序,然后返回一个可迭代的对象。 This way you will be able to loop it. 这样,您就可以循环播放它。

Another approach is using a simple array with the keys, Etc, however this approach (iterable object) is cleaner. 另一种方法是使用带有键Etc的简单数组,但是这种方法(可迭代对象)更干净。

Let's create a function called sortAsIterable(..) , this function will return an iterable object of players. 让我们创建一个名为sortAsIterable(..)函数,该函数将返回玩家的可迭代对象。

Important: Be careful with IE 重要说明:小心使用IE

 let players = { buggy: { name: 'John', surname: 'Cocroach', rank: 872 }, valentino: { name: 'Antonio', surname: 'Valencia', rank: 788 }, tommy: { name: 'Tom', surname: 'Witcher', rank: 101 },}; function sortAsIterable(obj) { let sortedKeys = Object.keys(obj).sort(function(a, b) { return obj[a].rank - obj[b].rank; }); let myIterable = {}, index = 0; myIterable[Symbol.iterator] = function*() { for (let k of sortedKeys) yield {[k]: obj[k]}; }; return myIterable; } // Object sorted. let sortedPlayers = sortAsIterable(players); players.buggy.name = "Ele"; // This is to illustrate the live access to the values. // Now you can iterate them . // You can do this as much as you want, for example in different parts of your code. for (let player of sortedPlayers) console.log(player); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

Resources 资源

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

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