Skip to content

Instantly share code, notes, and snippets.

@michael
Created June 28, 2011 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michael/1051399 to your computer and use it in GitHub Desktop.
Save michael/1051399 to your computer and use it in GitHub Desktop.
ES6 Suggestion: Deterministic property order in Javascript objects
var json = '{"d": "D", "c": "C", "b": "B"}';
var obj = JSON.parse(json);
Object.keys(); // => ["d", "c", "b"]
// Insertion order matters
obj["a"] = "A";
// Insertion at a certain position? (e.g. after key)
obj["x", "c"] = "X";
// Iteration
obj.forEach(function(value, key) {
console.log(key); // => "d", "c", "x", "b", "a"
});
Object.keys(obj); // => ["d", "c", "x", "b", "a"]
JSON.stringify(obj); // => '{"d": "D", "c": "C", "x": "X", "b": "B", "a": "A"}'
@michael
Copy link
Author

michael commented Jun 28, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment