DEV Community

[Comment from a deleted post]
 
ortonomy profile image
🅖🅡🅔🅖🅞🅡🅨 🅞🅡🅣🅞🅝 • Edited

Not sure how spread makes it more clear?!?! Array.prototype.slice is literally intended and is know to make a copy of an array....

From MDN:

The slice() method returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included) where start and end represent the index of items in that array. The original array will not be modified.

Using the spread operator in no way marks an intent to copy. The only thing that says that in your code is the comment. 🤣

And if you understand splice, it modifies the array in place....

so, three operations:

COPY : const _copy = x.slice()
DELETE: _copy.splice(y, 1)
RETURN

It's pretty damn easy to understand. Wrap it in a function if you want safelyDeleteFromArrayAtIndex(array, index)