Created
June 28, 2011 15:30
-
-
Save michael/1051399 to your computer and use it in GitHub Desktop.
ES6 Suggestion: Deterministic property order in Javascript objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Background: http://substance.io/#michael/data-js/_section_aac6c7ea55131dc591ed8cafd8eedb13