Proposal: Specifying an Index in Array Literals

Jordan Harband ljharb at gmail.com
Thu Jan 19 05:34:52 UTC 2017


`return Object.assign([...people], { [idx]: transform(people[idx]) })`
should work for you with no additional syntax.

On Wed, Jan 18, 2017 at 8:30 PM, Jeremy Martin <jmar777 at gmail.com> wrote:

> 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
>
> _______________________________________________
> 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/20170118/cec979d6/attachment.html>


More information about the es-discuss mailing list