Try this :
The splice() method adds and/or removes elements to/from an array, and returns the removed element(s).
Syntax:
array.splice(index,howmany,element1,.....,elementX)
index : Required. An integer that specifies at what position to add/remove elements
howmany : Required. The number of elements to be removed. If set to 0, no elements will be removed
element1, ..., elementX : Optional. The new element(s) to be added to the array
Example1 :
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.write("Added: " + fruits.splice(2,0,"Lemon") + "<br />");
document.write(fruits);
Example2 :
var fruits = ["Banana", "Orange", "Apple", "Mango"];
document.write("Removed: " + fruits.splice(2,1,"Lemon") + "<br />");
document.write(fruits);
Output1 :
Added:
Banana,Orange,Lemon,Apple,Mango
Output2 :
Remove one element from position 2, and add a new element to position 2 in the array:
Removed: Apple
Banana,Orange,Lemon,Mango