I have been using splice method to replace array values but recently learned a newer way for this purpose, thus sharing it with the rest.
...
For further actions, you may consider blocking this person and/or reporting abuse
Nice on, Shameel! 🔥
Thanks! =D
It's true the API is a little cumbersome (often you want to either remove elements or add them, not do both in the same operation), but the main issue with it is in-place mutation. Sometimes that can be useful, but more often than not you want a copy of the array instead.
Array#withisn't a drop-in immutable replacement forArray#splice; it only covers theArray#splice(existingIndex, 1, singleReplacement)case. It's a much better API for that use case, but not as versatile asArray#splice.There is now a drop-in immutable replacement though, which is called
Array#toSpliced. It has an identical API toArray#splice, except it doesn't mutate the original array and returns the copied array with modifications.