JS Built-ins
← Array197 / 226
ArrayArray.prototype.toSplicedbeginner

Remove Element Immutably

πŸ“– Usage Example
const arr = [1, 2, 3, 4, 5]
arr.toSpliced(1, 2)  // β†’ [1, 4, 5]
arr  // β†’ [1, 2, 3, 4, 5]  (unchanged)

Use toSpliced() to remove elements and return a new array, leaving the original intact.

Array.prototype.toSpliced

toSpliced is the immutable version of splice. It returns a new array with elements removed or replaced, leaving the original untouched.

Implement removeImmutable which returns a new array with the element at index removed.

πŸ’‘ Hints (2)
  • β€’ toSpliced(index, deleteCount) removes deleteCount elements starting at index
  • β€’ Pass 1 as deleteCount to remove exactly one element