0

I have the following object:

obj = {
 a: 1,
 b2: 2,
 ...
}

I need to replace prop 'a' with 'a1' and the new prop to be the very first. Is it possible? The code below doesn't fit as

obj.a1 = 1
delete obj.a

results in

obj = {
  b2: 2,
  ...
  a1: 1
}
  • 2
    The order of properties in objects should be irrelevant. – Sebastian Speitel Aug 02 '18 at 07:56
  • @RobbyCornelissen They do have an order, it's insertion order. At one point property order was undefined, recent JS has rectified that. So if property order is important to the OP there is a way of doing it. – Keith Aug 02 '18 at 07:59
  • i don't think it a duplicate of the mentioned question. – Nina Scholz Aug 02 '18 at 08:00
  • @Keith That is not generally true. See [Does ES6 introduce a well-defined order of enumeration for object properties?](https://stackoverflow.com/questions/30076219/does-es6-introduce-a-well-defined-order-of-enumeration-for-object-properties/30919039) – str Aug 02 '18 at 08:19
  • @str Be interesting to see what modern browser it's not true, JK.Crowder in that link created a fiddle to test. – Keith Aug 02 '18 at 08:33
  • @Keith It is not specified as such, so you should not rely on it. – str Aug 02 '18 at 08:41
  • @str Personally I don't, never really need to worry about the ordering of an object,. If ordering is important I would would use an array. But having part of the spec dictating ordering of Object's, and then totally dis-regarding this when doing `Object.key`s etc. seems, odd. So was just wondering if there was any browsers that did. Also in regards to my answer it's of course still true, as I said nothing about iterating. – Keith Aug 02 '18 at 08:47
  • @Keith It is not only about iterating. You wrote "They do have an order, it's insertion order." which is not true. `Object.keys({a:true,0:true})` returns `['0', 'a']`. There are more rules than just insertion order. – str Aug 02 '18 at 08:52
  • @str If using numbers as keys there not treated as keys, but as array index specifiers,.. To be more key based -> `Object.keys({aa:true, a0: true})`, But my point is more -> `var a = {}; a.z = 1; a.k = 2; a.a = 3;` would be `{z: 1, k: 2, a: 3}` on all modern browsers,.. But on older browsers that bit was not specified. – Keith Aug 02 '18 at 09:04
  • @Keith I know that. It is exactly why I mentioned that your comment is "not generally true" and linked to an answer that is outlining the exact specification. – str Aug 02 '18 at 09:17
  • @str Not sure how my comment was not true,.. Maybe you mean I wasn't specific enough,.. Would it have been better if I said, The order of index specifier & keys are defined, keys are in insertion order, and index specifiers in numeric order?. Not trying to argue here, but I'm just not sure why you think what I said is generally false, when to me it's more generally true. Like you say iteration by specification is something else, but even then I just wondered if you knew of any browsers where iteration is different to object view, not that I need to rely on it, but more out of curiosity. – Keith Aug 02 '18 at 09:49
  • @Keith I never said it is "generally false" but instead "not generally true". It might be true for some use cases but not for others as the insertion order is only part of the actual order. Thus "it is insertion order" is not generally true (or not specific enough) and I linked to the answer with more detail. There are differences in the browser implementations, even the comment you mentioned says "Firefox's order certainly used to be different". You can certainly test different browsers but I wouldn't bother as they might change their behaviour at any time. After all, it is not specified. – str Aug 02 '18 at 09:56
  • @str `After all, it is not specified.`, but isn't that talking about iteration? I'm totally with you when it's about iteration `Object.keys` / `for in` etc. But lets create example, lets say I wanted to make a JSON beautifier, and for some odd reason I wanted the keys sorted / (maybe even in reverse). By specification I could do this by converting into an array, sort, and re-insert into the object. And this by specification would be 100% correct, is that right?. Or have I missed something, sorry if this is going on, but I would really like to know what I'm misunderstanding. – Keith Aug 02 '18 at 10:11
  • @Keith In most cases, yes. But not for numeric properties and properties with Symbols. For example `JSON.stringify({a:1,1:1})` will return `{"1":1, "a":1}`. – str Aug 02 '18 at 10:32

0 Answers0