There is an object

var obj = { 3: 3, 2: 2, 1: 1 }; 

When I try to display it for viewing, it sorts by ascending key.

 Object {1: 1, 2: 2, 3: 3} 

How to make a reverse object? What would he be like in the original form?

By default, creating an object randomly specifying keys as numbers

 var obj2 = {5:5, 2:2, 3:3}; 

Interpreter as I understand it, sorts it ascending. So I thought that there could be some constant, or a property to override ...

  • and why is it needed? what purpose? - Mikhail Vaysman
  • An object is an unordered structure by design. Order needed - use an array. Yes, there is a certain default order of properties in the object, but by relying on it we roughly violate the semantics - Duck Learns to Hide
  • Just understand that when you do var obj = {}, you create a disordered structure and in what order you add properties to it - by and large everyone doesn’t care - Duck Learns to Hide
  • About the fact of the order of the object in fact, they have already answered here, I'll find a conductor - Duck Learns to Hide
  • Possible duplicate question: What is the principle of the FOR IN loop bypassing an array? - Duck Learns to Hide

1 answer 1

No ES6 sets the order in which properties are searched:

  1. Non-negative indices ascending
  2. All other string keys in the order of addition
  3. Symbols if available for this brute force

You can always use strings instead of numbers:

 var obj = { _3: 3, _2: 2, _1: 1 };