Accessing (n)th key from an object

T.J. Crowder tj.crowder at
Tue Apr 24 15:06:56 UTC 2018

In that use case, I'd just remember those two objects from when you
converted the array to an object, e.g.:

const obj = Object.create(null);
for (const item of theArray) {
    obj[item.key] = item;
const firstItems = theArray.slice(0, 2);

...then use `firstItems` for showing the first two. Also has the
advantage that you can easily change how many you show before the "See

-- T.J. Crowder

On Tue, Apr 24, 2018 at 3:41 PM, somonek <somonek at> wrote:
> 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.

More information about the es-discuss mailing list