How is possible to change 3/4 elements? Expected output is [1,2,4,3,5]
let list = [1,2,3,4,5];
const removeElement = list.indexOf(3); // remove number 3
list.slice(0, removeElement).concat(list.slice(removeElement+1)) // [1,2,4,5]
...next push number 3 after number 4 without splice
Array.prototype.splice()
is not immutable. It modifies the given array. Looks like OP is looking for an immutable solution. – Employeeslice().splice()
works though, for immutability – Aedesslice().splice()
(copying and then using splice) doesn't work because splice returns the removed elements, not the updated array – Landaulet