insertAtImmutable
Insert(Immutable) the given values at the specified index.
Use Array.prototype.slice()
with an appropriate index and a delete count of 0
, spreading the given values to be inserted.
typescript
const insertAtImmutable = (arr: any[], i: number, ...v: any[]) => {
arr.splice(i + 1, 0, ...v);
return arr;
};
typescript
insertAtImmutable([1, 2, 3, 4], 2, 5); // [1, 2, 3, 5, 4]
insertAtImmutable([2, 10], 0, 4, 6, 8); // [2, 4, 6, 8, 10]
// Insert from behind
insertAtImmutable([4, 5], -1, 1, 2, 3); // [1, 2, 3, 4, 5]