Open elHornair opened 3 years ago
Just realised that there is a much simpler way to do it:
console.log([...drinks.slice(0, index), 'Pizza', ...drinks.slice(index)]);
Opposed to splice
, slice
is immutable. I'm assuming this is what you wanted to do in the first place and the error is only a typo. Beware that the index correction (-1) is now not needed anymore though.
The code shown in the explanation video is not in fact immutable.
Incorrect code:
Correct code:
Explanation: As
splice
mutates the original array, before the spread operator spreads it, the original array gets mutated nevertheless. Solution: copy original array with spread operator, splice it, then spread the result.