Using JavaScript, I am trying to create the following object:
{name: "John Doe", grade: "3", 6: "six"}
Note that the key for the third element is a number, 6
.
I am creating the object as follows:
var myObject = {};
// Some code goes here
myObject["name"] = "John Doe";
// Some more code goes here
myObject["grade"] = "3";
// And even more code here
myObject["6"] = "six";
Now, the problem is the object gets constructed as follows
{6: "six", name: "John Doe", grade: "3"}
Note that the element 6: "six"
moved to the front of the object even though it was added last.
I noticed that all elements that have numbers as their keys get added to the front even though they get added after other elements. How I ensure that the elements are not re-arranged and keep my original order?
Thanks.