简体   繁体   English

如何用数字键对对象进行降序排序?

[英]How to sort object with numeric keys in descending order?

I'm having an issue where the code I have written seems to do two very odd things. 我遇到了一个问题,即我编写的​​代码似乎做了两件非常奇怪的事情。 when running line by line the object is constructed in the object in the correct war but as soon as it's out of the for loop the object that's been created seems to default back to the keys being in ascending order? 当逐行运行时,对象是在正确的战争中构造在对象中的,但是一旦它脱离for循环,则所创建的对象似乎默认会恢复为按升序排列的键?

The object I sent into the function looks as follows: 我发送给函数的对象如下所示:

{
   0: 'Hello',
   20: 'World',
   40: 'Test'
}
const reverseObject = (object) => {
  const newObject = {};
  Object.keys(object).reverse().forEach((key) => {
    newObject[key] = object[key];
  });
  return newObject;
};

what I was expecting to happen here was an object that looked like: 我期望在这里发生的是一个看起来像这样的对象:

{
   40: 'Test'
   20: 'World',
   0: 'Hello',
}

the newObject looks like this during the loop 在循环期间,newObject看起来像这样

{
   40: 'Test'
   20: 'World',
   0: 'Hello',
}

but on the return line looks like 但在返回线上看起来像

{
   0: 'Hello',
   20: 'World',
   40: 'Test'
}

Im unsure if something deep down in javascript is restricting this sort of use case but an API I'm trying to use slightly differently to the way its setup takes an object with keys that look like this for the order. 我不确定javascript中是否有某些东西限制了这种用例,但我尝试使用的API与设置它的对象采用带有类似于该命令键的对象的方式稍有不同。

Any help would be much appreciated if you can think of a way to sort this as intended. 如果您能想到一种按预期进行排序的方法,将对您的帮助非常感谢。 Look forward to the responses. 期待得到答复。

I dont belive key ordering is deterministic. 我不相信按键顺序是确定性的。 Chrome, at least, always seems to order them sequentially: 至少,Chrome似乎总是按顺序对其进行排序:

Object.keys({20: 'Hello', 30: 'There', 0: 'World'});
>> ["0", "20", "30"]

You can work around the auto-sorting of object keys by making the keys strings. 您可以通过使键字符串变通来解决对象键的自动排序。 Technically @Dacre Denny is correct that the ordering of keys in JavaScript objects is not guaranteed BUT afaik do all browser keep the ordering of keys as specified when the Object was created EXCEPT they are numeric! 从技术上讲,@ Dacre Denny是正确的,因此不能保证JavaScript对象中键的顺序,但是所有浏览器都应按创建对象时指定的键顺序来保留键的顺序,除非它们是数字!

 const obj = { 0: 'Hello', 20: 'World', 40: 'Test' }; const objStrKeys = { '_0': 'Hello', '_20': 'World', '_40': 'Test' }; const reverseObject = (object) => { const newObject = {}; Object.keys(object).reverse().forEach((key) => { newObject[key] = object[key]; }); return newObject; }; console.log('obj', reverseObject(obj)); console.log('objStrKeys', reverseObject(objStrKeys)); 

not that it's not enough to "just" wrap the keys in quotes and leave them numeric, they require some kind of string prefix/suffix. 仅仅“把”键用引号引起来并使其保持数字是不够的,它们还需要某种字符串前缀/后缀。 And yes, I'm aware that this might not work depending on your use-case and that it is rather ugly, but if you really need the properties in reverse order, this is your option =)... 是的,我知道这可能无法使用,具体取决于您的用例,而且很难看,但是如果您确实需要反向的属性,则可以选择=)...

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

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