Accessing (n)th key from an object
Jordan Harband
ljharb at gmail.com
Tue Apr 24 18:07:53 UTC 2018
Regardless of what's in the spec, relying on objects having an order among
their properties violates the conceptual mental model of objects: a bag of
unordered key/value pairs.
If you want to convert an array - the best way to preserve order - into an
object for "performance" reasons, then you may also want to preserve an
array of IDs so that ordering can be relied upon.
On Tue, Apr 24, 2018 at 10:26 AM, T.J. Crowder <
tj.crowder at farsightsoftware.com> wrote:
> On Tue, Apr 24, 2018 at 5:58 PM, Augusto Moura <augusto.borgesm at gmail.com>
> wrote:
> > Assuming `Object.keys` has the same order as a simple `for..in` (as
> stated
> > in
> > mdn)[https://developer.mozilla.org/en-US/docs/Web/
> JavaScript/Reference/Global_Objects/Object/keys]...
>
> We'll have to fix that page, the spec does not define the order in which
> `for-in` will enumerate property names. (It does define the order in which
> `Object.keys` will provide the property names, now, but not `for-in`.) That
> said, all modern and modern-ish implementations I've tested (to be fair,
> just V8, SpiderMonkey, Chakra, and IE11's JScript) do indeed follow the
> defined order with `for-in` as well, with an object's own properties coming
> before inherited ones - http://jsfiddle.net/arhbn3k2/1/ (IE11-friendly
> version without Symbols: http://jsfiddle.net/arhbn3k2/5/). (Provided I
> haven't screwed up that test.) But it's specifically *not* specified - from
> [EnumerateObjectProperties][1] (the op `for-in` uses to initialize its
> loop):
>
> > The mechanics **and order** of enumerating the properties **is not
> specified** but must conform to the rules specified below.
>
> *(my emphasis)* It says it has to use [[OwnPropertyKeys]] to get the own
> property keys at each level, but doesn't -- as far as I can tell -- define
> the sequence of own vs. inherited properties.
>
> `Object.entries` follows the order (own properties only):
>
> ```js
> const [first, second, ...rest] = Object.entries(obj);
> ```
>
> ...but I don't think that, or `keysIterator`, really helps somonek vs.
> what he's doing now (`Object.keys`).
>
> -- T.J. Crowder
>
> [1]: https://tc39.github.io/ecma262/#sec-enumerate-object-properties
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20180424/78605fdd/attachment-0001.html>
More information about the es-discuss
mailing list