// The original array var array = ["one", "two", "four"]; // splice(position, nrOfItemsToRemove, item) array.splice(2, 0, "three"); array; // ["one", "two", "three", "four"]
If you aren't adverse to extending natives in JavaScript, you could add this method to the Array prototype:
Array.prototype.insert = function (index, item) {
this.splice(index, 0, item);
};
No comments:
Post a Comment