Proposal: Specifying an Index in Array Literals

Jeremy Martin jmar777 at gmail.com
Thu Jan 19 04:30:07 UTC 2017


I just came across a use case where I wanted to create a shallow copy of an
array, while also updating the value at a particular index.

Is it feasible to extend the spread syntax for array literals in a manner
that supports updating values at a given index, similar to how the object
literal syntax supports updating a particular key?

Here's a quick example to illustrate:

    const idx = people.findIndex(/* some predicate */);

    if (idx >= 0) {
      return [
        ...people,
        [idx]: { ...people[idx], /* updates */ }
      ];
    }

I suppose this implies a way to specify indexes in array literals in the
general case, but I can't think of any particularly compelling use cases
w/out the spread operator involved.

Thanks!

-- 
Jeremy Martin
@jmar777
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20170118/9472a825/attachment.html>


More information about the es-discuss mailing list