0
z = {
    Asf:46,
    sage:46,
    fdfds:58,
  };
  z["619"] = 48;
  console.log(z);

// I try to add to the bottom of the object but it add to the top the object it just because of the string passing in terms of numbers but I tried other solution it won't work

  • object keys are generally not "ordered" - though, you'll find they usually are, numeric first, followed by non numeric in lexical order - never rely on that though – Bravo Mar 18 '22 at 09:39
  • @Bravo - Not "usually," the order has been clearly specified since 2015. But yes, don't rely on it, the rules are complex and they depend on both how the object was created and what the actual text of the property names is. – T.J. Crowder Mar 18 '22 at 09:41
  • @T.J.Crowder I never realised it was formally **specified** - I'm sure, until relatively recently, there was some way to get the keys in "creation" order - maybe that was 7 years ago, time flies when you're old :D – Bravo Mar 18 '22 at 09:44
  • 1
    okay thank you , I never rely on this again ... @bravo – Ajay Kumar Mar 18 '22 at 09:47
  • @Bravo - LOL Yes, ES2015 set the order for "own" properties and subsequent specs have nailed down the order of properties even when inherited. For **own** properties, it's properties that look like array indexes first, numerically, followed by others in (as you say) creation order. – T.J. Crowder Mar 18 '22 at 09:48
  • At least related? https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order – T.J. Crowder Mar 18 '22 at 09:50
  • @T.J.Crowder - ahh, of course, why I said "lexical" instead of "insertion" in my original comment is beyond me - given that, I was actually correct (though, I never knew it was in a specification – Bravo Mar 18 '22 at 10:02
  • @Bravo - :-) It's temporal rather than lexical, but lexical is temporal in an object literal. :-) – T.J. Crowder Mar 18 '22 at 10:09
  • @T.J.Crowder - what chapter is that covered in :p – Bravo Mar 18 '22 at 10:10
  • 1
    @Bravo - LOL It's in Chapter 5. – T.J. Crowder Mar 18 '22 at 10:11

1 Answers1

0

I try to add to the bottom of the object but it add to the top the object

The order of properties in an "ordinary" JavaScript object such as yours is set out by the specification. For the properties in your example, the 619 property comes first in that order, because it's an "array index" property name, those come before non-array index property names.

You can't change that, but more importantly, relying on object property order is almost always a bad idea. If you want order, use an array or a Map. For instance, in your case, you might want a Map, which is ordered purely by the order in which you add entries to it:

const map = new Map([
    ["Asf", 46],
    ["sage", 46],
    ["fdfds", 58],
]);
map.set("619", 48);

Live Example:

const map = new Map([
    ["Asf", 46],
    ["sage", 46],
    ["fdfds", 58],
]);
map.set("619", 48);
console.log([...map]);
.as-console-wrapper {
    max-height: 100% !important;
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875