anArray.splice(startIndex, length, newElement1, newElement2, ...)
comments | Javascript source | Execution result in your current browser |
initialize array | var a1=[1,2,3,4,5,6];
document.write(a1);
|
|
Remove the first 3 elements | a1.splice(0,3);
document.write(a1);
|
|
Insert 2 elements at the begining | a1.splice(0,0, "One", "Two");
document.write(a1);
|
|
Delete 2 elements following the first element | a1.splice(1,2);
document.write(a1);
|
|
Update the last 2 elements | a1.splice(-2,2,["Two","Three"]);
document.write(a1);
|
|
replace with new array | a1.splice(0,a1.length, 1,2,3,4,5);
document.write(a1);
|
|