Accessing (n)th key from an object

T.J. Crowder tj.crowder at farsightsoftware.com
Tue Apr 24 15:22:45 UTC 2018


On Tue, Apr 24, 2018 at 4:06 PM, David Teller
<dteller at mozilla.com> wrote:
>
> Doing this sounds pretty fragile. Do you have any guarantee that the
> order of fields is the same as the order of the array? Even if that's
> the case in your scenario, it's pretty uncommon.

If we make a couple of assumptions:

1. He's adding the properties to a newly-created object in array order
2. None of the keys is an [integer index][1]

...then yes, the order is guaranteed (even for `Object.keys`, nowadays
-- that wasn't true in ES2015, not sure when it changed and the ECMA
site is down so I can't check the 2016 or 2017 snapshots [note to
self: keep local copies!]).

Agreed it's not the best way to go about it, though. :-)

-- T.J. Crowder

[1]: https://tc39.github.io/ecma262/#sec-object-type


More information about the es-discuss mailing list