Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Insert and remove elements at positions in an Array in Javascript
Array.prototype.remove = function(index) {
delete this[index];
for (var i = index + 1; i < this.length; i++)
this[i-1] = this[i];
Array.prototype.insert = function(index, value) {
for (var i = 0; i < index + 1; i++)
this[i-1] = this[i];
this[index] = value;

This comment has been minimized.

Copy link
Owner Author

@donnaken15 donnaken15 commented Oct 25, 2019

Use as follows:

yourarray = ['a', 'b', '1', 'c', 'd', 'e', 'f']

yourarray = ['a', 'b', 'd', 'e', 'f']
yourarray.insert(2, 'c');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.