Skip to content

maintaining order of keys during iteration #106

@nickdesaulniers

Description

@nickdesaulniers

keys in JSON are not sorted, but ordered based on definition. This becomes apparent when iterating keys of an object. For example, in JavaScript, with an object literal:

x = { a: 0, b: 1 }
y = { b: 0, a: 1 }
for (var key in x) console.log(key)
for (var key in y) console.log(key)
for (var key in JSON.parse(JSON.stringify(x))) console.log(key)
for (var key in JSON.parse(JSON.stringify(y))) console.log(key)

prints

a
b
b
a
a
b
b
a

For example, I believe most JS JITs keep separate shapes for object with the same properties but defined in different orders.

Metadata

Metadata

Assignees

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions