Sub topic 5
Inserting, removing, and replacing with toSpliced
This chapter assumes you already know how Array.prototype.splice works and what its start, deleteCount, and items arguments do. You should also know that splice changes the array you call it on.
toSpliced is the copying version of splice. It takes the same three kinds of arguments, but it changes the array in only one way. It leaves the original untouched and returns the new array that results from the edit.
The signature
array.toSpliced(start, deleteCount, ...items)startis the index where the edit begins. A negative value counts back from the end of the array.deleteCountis how many elements to remove fromstartonward. If you leave it out, everything fromstartto the end is removed.itemsare the values to insert atstart, after the deletion.
One call to toSpliced can do three jobs, depending on the arguments you pass.
| Goal | deleteCount | items | Example |
|---|---|---|---|
| Insert | 0 | one or more | `months.toSpliced(1, 0, "Feb")` |
| Remove | 1 or more | none | `months.toSpliced(2, 2)` |
| Replace | 1 or more | one or more | `months.toSpliced(1, 1, "Feb")` |
A worked example
const months = ["Jan", "Mar", "Apr", "May"];
const inserted = months.toSpliced(1, 0, "Feb");
console.log(inserted); // ["Jan", "Feb", "Mar", "Apr", "May"]
const removed = months.toSpliced(2, 2);
console.log(removed); // ["Jan", "Mar"]
const replaced = months.toSpliced(1, 1, "Feb");
console.log(replaced); // ["Jan", "Feb", "Apr", "May"]
console.log(months); // ["Jan", "Mar", "Apr", "May"]Each call starts from the same unchanged months array. With splice, each call would have worked on the result of the previous one, and you would need to keep track of the array's state between calls.
The return value is different
This is the most common mistake when moving from splice to toSpliced. splice returns the elements it deleted. toSpliced returns the whole resulting array.
const months = ["Jan", "Mar", "Apr", "May"];
const copy = [...months];
const deleted = copy.splice(1, 1, "Feb");
console.log(deleted); // ["Mar"]
console.log(copy); // ["Jan", "Feb", "Apr", "May"]
const result = months.toSpliced(1, 1, "Feb");
console.log(result); // ["Jan", "Feb", "Apr", "May"]Negative start and omitted deleteCount
toSpliced follows the same index rules as splice:
const months = ["Jan", "Mar", "Apr", "May"];
console.log(months.toSpliced(-1, 1)); // ["Jan", "Mar", "Apr"]
console.log(months.toSpliced(2)); // ["Jan", "Mar"]
console.log(months.toSpliced(-2, 0, "Mid")); // ["Jan", "Mar", "Mid", "Apr", "May"]A start past the end of the array is clamped to the length, so the items are appended. Unlike with, toSpliced never throws for an out-of-range index.
Exercise
Given const letters = ["a", "b", "c", "d"];, what do letters.toSpliced(1, 2, "x") and letters contain after the call?
Show answer
The call returns ["a", "x", "d"], because it removes "b" and "c" and inserts "x" at index 1. letters is still ["a", "b", "c", "d"].
Exercise
You are replacing const removed = list.splice(i, 1); with toSpliced. You still need the removed element and you must not mutate list. Write the replacement.
Show answer
const removed = list.slice(i, i + 1); const next = list.toSpliced(i, 1); The slice call reads the element that will be removed, and toSpliced returns the new array without it.