In JavaScript, you can usually do anything with the provided arrays and objects.
To add elements, use Array.prototype.push
.
var array = ["12", "34", "56"];
array.push("78", "90");
To properly remove elements without leaving holes, you need to find their index (you can use Array.prototype.indexOf
in good browsers, there is a shim on the page for IE) and use Array.prototype.splice
.
array.splice(array.indexOf("34"), 1);
array.splice(array.indexOf("56"), 1);
Note that if you already know the index and the count, you can just do
array.splice(1, 2); //Removes 2 elements starting at index 1
Also note that splice
can also add elements with the same call. Just put them as the last parameters. That means you could do this:
var array = ["12", "34", "56"];
array.splice(1, 2, "78", "90"); //array is now ["12", "78", "90"]