Javascript is an awesome language but lacks some useful API’s such as for Arrays. Insert and delete at a particular position are two very basic array operations that can be done by using the splice method. I didn’t even know what splice meant till I bumped across Javascript Arrays. Now Array can be easily extended to do that using prototype, but a better home for such methods and wrappers is in the core language itself.
For the time being, it’s easy to create wrappers that use splice under the hood.
// Insert element at the given index in array
Array.prototype.insert = function(element, index) {
this.splice(index, 0, element);
}
// Remove the element at the given index
Array.prototype.remove = function(index) {
this.splice(index, 1);
}