The complement of the at prototype method, but for setting values, instead of getting them.

Apologies in advance if this is a dupe

arr.with(-1, 2) is what you want.

This can be a useful addition to the MDN docs for both and Array.prototype.with—welcoming PRs ;)

1 Like

Not quite, but very close! I would expect setAt to be mutating, not copying.

I understand that copying is usually good practice, especially in FP, but there are cases when it's preferable to mutate in-place.

I agree! I wonder why they didn't link these 2 together (in a similar way as reverse and toReversed) before

There won't be consensus, I think, to ever add another mutating method to Array.prototype.

Currently, there is a.splice(index, 1, value). Not quite as intuitive, but it least it does the job (unless you have specific requirements for the returned value).


Because when I wrote the with page, I did not think too deeply about using negative indices. Its function is too obvious in the context of change-array-by-copy.

1 Like