In this article, we explore two approaches of replacing items in an array, using the mutable Array.splice() or using the immutable Array.slice() + spreading
We can use two approaches, the mutable, Array.splice() approach, or the immutable, Array.splice() with the spread operator approach. Choosing between the two approaches depends on the context and the environment you’re working in.
The first approach is quite simple, we just mutate the same array. The Array.splice() method is used to extract items from an array, but also it supports the option of replacing the extracted items with new ones: