-2

Here's my ReactNative code.

// measurements are loaded from a jsonb column in postgres (supabase)
const measurements = loadFromDb(); 
console.log('measurements:', measurements);
console.log('typeof measurements:', typeof measurements);
console.log('Object.keys:', Object.keys(measurements));
console.log('JSON.stringify(measurements)', JSON.stringify(measurements))

const jsonMeasurements = {"Hip": "33", "Skirt length": "20", "Thigh": "44", "Top length": "", "Top waist": "", "Waist": "33"};
console.log('Json Object.keys:', Object.keys(measurements));

Outputs

measurements: {"Hip": "33", "Skirt length": "20", "Thigh": "44", "Top length": "", "Top waist": "", "Waist": "33"}
typeof measurements: object 
Object.keys: ["Hip", "Thigh", "Waist", "Top waist", "Top length", "Skirt length"]
JSON.stringify(measurements): {"Hip":"33","Thigh":"44","Waist":"33","Top waist":"","Top length":"","Skirt length":"20"}  

JSON Object.keys: ["Hip", "Skirt length", "Thigh", "Top length", "Top waist", "Waist"]

Why are the keys rearranged when loaded from supabase but not from an inline json?
As you can see if I inline init the measurements object with json object, the order is not altered.

My guess is measurements object loaded from db is not a object.

Null Head
  • 2,877
  • 13
  • 61
  • 83
  • 1
    The keys in a JavaScript object are **never** sorted. If you want to preserve the order you should use an array – Christian Vincenzo Traina Aug 16 '23 at 10:32
  • Also, there is no something like a JSON object. A JSON is always the serialization of an object – Christian Vincenzo Traina Aug 16 '23 at 10:32
  • If they are never sorted, why are they sorted as you can see in the log? – Null Head Aug 16 '23 at 10:41
  • What does `loadFromDb` do exactly? It certainly makes a network request (e.g. `fetch()`) and is probably the one generating the object you receive. What input does it have to produce that object? And yes there are various sorting rules for various ways of accessing object's keys. – Kaiido Aug 16 '23 at 10:59
  • @NullHead I mean they're never **internally** sorted. When you query the keys with `Object.keys`, there will obviously be an order. But that order, even if it can be predictable in some JS engines, it's not specified in EcmaScript language specs thus it's not reliable – Christian Vincenzo Traina Aug 16 '23 at 11:08
  • Or [Does ES6 introduce a well-defined order of enumeration for object properties?](https://stackoverflow.com/questions/30076219/does-es6-introduce-a-well-defined-order-of-enumeration-for-object-properties) – jabaa Aug 16 '23 at 11:21
  • @ChristianVincenzoTraina It is well defined and reliable. – jabaa Aug 16 '23 at 11:22
  • But it's also possible that something else changes the the order. The values are queried from a database. The values are probably serialized to JSON. Then, the JSON data is parsed to a JavaScript object. Serializing and parsing can change the property order, since JSON doesn't guarantee a specific order. – jabaa Aug 16 '23 at 11:27
  • I dont want any order change. I just want them to appear as they are in the original object but Object.keys and JSON.stringify are rearranging the order. – Null Head Aug 16 '23 at 12:23
  • Why the down vote? Why close? – Null Head Aug 16 '23 at 12:24
  • 1
    I assume because you aren't listening. There is no meaningful "original order" and certain operations are defined in the language spec as not preserving whatever order is there. If you want to access object keys in a specific order, you need to specify your own ordering in an array. – Richard Huxton Aug 16 '23 at 13:52
  • The question tagged as similar clearly says ordering doesnt follow insertion order. I am not inserting. Its just a static value I am trying to iterate over. – Null Head Aug 16 '23 at 23:12
  • 1
    Your question doesn't show where the object comes from and how it's created. Can you provide a [mcve]? Assuming JSON is involved, you can't expect any order. Parsers and serializers are allowed to change the order. _"As you can see if I inline init the measurements object with json object, the order is not altered."_ That's not JSON. It's a JavaScript object literal and that makes a big difference in this context. – jabaa Aug 16 '23 at 23:43
  • 2
    Despite the title of your question, the problem does not appear to have anything to do with JavaScript's `Object.keys` or `JSON.stringify`. Read https://www.postgresql.org/docs/current/datatype-json.html: "*`jsonb` does not preserve white space, **does not preserve the order of object keys**, and does not keep duplicate object keys.*". Use `json` if you must, but really, JSON objects are not meant to store ordered information. – Bergi Aug 17 '23 at 02:43
  • This makes sense. Thanks – Null Head Aug 17 '23 at 21:51

0 Answers0