Why splice
push adds an item at the end and pop takes the last one away. To insert or remove anywhere else, JavaScript has splice. It changes the array itself, so there is no new array to store.
How splice(start, count, item) works
The call goes to index start, takes out count items there, then puts in whatever items you list after count.
Insert: a count of 0
With a count of 0 nothing is taken out, so the call only inserts. The items from start on move one place to the right.
let pets = ["Rex", "Milo"];
pets.splice(0, 0, "Luna");
console.log(pets);
[ 'Luna', 'Rex', 'Milo' ]
Remove: a count of 1 and no items
With a count of 1 and nothing after it, the call only removes. The items after the gap move one place to the left.
let pets = ["Luna", "Rex", "Milo"];
let removed = pets.splice(0, 1);
console.log(pets);
console.log(removed);
[ 'Rex', 'Milo' ]
[ 'Luna' ]
splice returns an array of the items it removed. Here that is ["Luna"]. When you only want the effect on the array, ignore the return value.
Remove and insert in one call
The count and the new items work together, so one call can replace items. Starting from ["a", "b"], splice(1, 1, "x", "y") takes out "b" and puts in "x" and "y", which gives ["a", "x", "y"].
Thinking in positions
Picture the array as a row of numbered seats. An insert opens a new seat at the position you name and every person behind it slides back one seat. A removal empties a seat and everyone behind it steps forward. The array has no gaps, so the indexes always run from 0 up to the length minus one.
This is why the order of your calls matters. Two splice calls on the same array give different results if you swap them, because the second call sees the array the first one left behind. When a task has two changes in a row, redo the picture after each one and read the index off the new picture.
The lesson's task
The array pets holds ["Rex", "Milo", "Luna"]. Bella has an appointment and goes second, at index 1. Then Milo goes home. The code prints how many pets wait once Bella is in, and at the end the pets left.
- Insert Bella: index 1, count 0, the item
"Bella". Milo and Luna each move one place to the right, so the array is["Rex", "Bella", "Milo", "Luna"]. - Remove Milo. He was at index 1, but Bella's insert pushed him to index 2. So the call is
splice(2, 1).
let pets = ["Rex", "Milo", "Luna"];
pets.splice(1, 0, "Bella");
console.log(pets.length);
pets.splice(2, 1);
console.log(pets[0], pets[1], pets[2]);
It prints:
4
Rex Bella Luna
Common mistakes
- Using an old index. After an insert or a removal, the items behind that spot have new indexes. Milo started at index 1, but after Bella went in,
splice(1, 1)would take Bella out and leave Milo in line. Count again after every change. - Forgetting the count.
splice(1)with no count removes everything from index 1 to the end. On["a", "b", "c"]it leaves["a"]. - Counting from the wrong side. The index is where the change starts, and the count says how far it reaches. Mixing up the two arguments, as in
splice(1, 0)when you meant to insert, changes nothing at all. - Expecting the array back. The return value is the removed items, not the changed array. Keep using the original variable.
Key takeaways
splice(i, 0, x)insertsxat indexi.splice(i, 1)removes the item at indexi.- The array changes in place, and the items behind the spot shift by one.
