Accessing (n)th key from an object

somonek somonek at gmail.com
Tue Apr 24 14:41:44 UTC 2018


The use case is:
(long story short)

I have an array of items coming from an api. They're converted to an object
having the ids as keys for faster access of single items and loaded to a
global store.
Then in React.js and I want to render only the first 2 items with a "See
more" that would render the rest of them.
It would be handy (and probably more performant when the object is big as
in my case) to have:

const product1 = myItems[in 0];
const product2 = myItems[in 1];

instead of converting everything to an array first and then accessing the
first items by index.




On Tue, Apr 24, 2018 at 4:18 PM, T.J. Crowder <
tj.crowder at farsightsoftware.com> wrote:

> On Tue, Apr 24, 2018 at 2:54 PM, somonek
> <somonek at gmail.com> wrote:
> > ...
> >
> > could
> > myObject[in 0] === 'one' // true
>
> What's the use case? Relying on the order of the properties in the object
> is almost always a bad idea (although if I read the current spec correctly,
> `Object.keys` is no longer exempt from order as it once was). The only time
> I've seen this done that seemed reasonable was when the object was known to
> have a single own enumerable property but that property's name was unknown
> (which was, in itself, an X/Y problem -- the real problem was why that name
> was unknown/varied at runtime).
>
> -- T.J. Crowder
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20180424/8cba9578/attachment.html>


More information about the es-discuss mailing list