Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何以最简单、最简单的方式(Javascript)按字母顺序排列对象_Javascript - Fatal编程技术网

如何以最简单、最简单的方式(Javascript)按字母顺序排列对象

如何以最简单、最简单的方式(Javascript)按字母顺序排列对象,javascript,Javascript,以以下对象为例: var object = { some_key: 'some value', some_other_key: 'some other value', AKey: 'notice that some will have underscores and some wont.' callBackl: 'http://google.ca' } 应成为: var object = { AKey: 'notice that some will have undersc

以以下对象为例:

var object = {
  some_key: 'some value',
  some_other_key: 'some other value',
  AKey: 'notice that some will have underscores and some wont.'
  callBackl: 'http://google.ca'
}
应成为:

var object = {
  AKey: 'notice that some will have underscores and some wont.'
  callBackl: 'http://google.ca'
  some_key: 'some value',
  some_other_key: 'some other value',
}
我还没有找到解决这个问题的洛达斯解决方案,我在堆栈上找到了很多解决方案,似乎。。。。复杂。也许这不是小事


想法?

编辑:当我第一次发布新定义的Javascript规范时,似乎我是不正确的(令我完全惊讶的是),Javascript对象确实有顺序()!尽管如此,我仍然不建议尝试对对象本身的元素进行排序,而是使用我在下面描述的键排序方法。keys(obj)以及其他一些边缘情况似乎仍然无法保证排序,我认为这是一个很好的理由,可以避免依赖对象的键的顺序


原文如下

除非我错误地理解javascript,否则javascript对象中的键没有保证顺序。这意味着,理论上,我可以创建对象

{'a':'hi', 'c':'hey', 'b':'hello'}
并且,在遍历它的键时,我可以得到任何可能的‘a’、‘b’和‘c’的顺序

如果需要以任何特定顺序显示对象,我建议访问对象的键并对其进行排序,然后按照新排序的键的排序顺序访问对象。
(可以在这里看到实现:)

重复@OliviaRuth此解决方案的问题在于其混乱。它不干净。真恶心,就是这样。似乎是最新的。那么古老,那么多的选票,看起来和你将得到的一样好。不一定是真的。ES6规范定义了一个订单。看@OliviaRuth就够了。我想“尽量不要在意”可能仍然是最好的建议。