Skip to content

Instantly share code, notes, and snippets.

Embed
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];
this.pop();
}
Array.prototype.insert = function(index, value) {
this.unshift(undefined);
for (var i = 0; i < index + 1; i++)
this[i-1] = this[i];
this[index] = value;
}
@donnaken15

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.remove(2);

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.