Proposal: Specifying an Index in Array Literals

# Jeremy Martin (7 months ago)

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 [
    [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.

# Jordan Harband (7 months ago)

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