Javascript examples for Array:splice
The splice() method adds/removes items, and returns the removed item(s).
This method changes the original array.
array.splice(index,count,item1, ..., itemX)
Parameter | Description |
---|---|
index | Required. An integer that specifies at what position to add/remove items, negative values starts from the end of the array |
count | Optional. The number of items to be removed. If set to 0, no items will be removed |
item1, ..., itemX | Optional. The new item(s) to be added to the array |
A new Array, containing the removed items (if any)
remove two elements from the array
<!DOCTYPE html> <html> <body> <button onclick="myFunction()">Test</button> <p id="demo"></p> <script> var fruits = ["a","b","c","d","e", "Kiwi"]; document.getElementById("demo").innerHTML = fruits; function myFunction() {//w w w . java 2s.c om fruits.splice(2, 2); document.getElementById("demo").innerHTML = fruits; } </script> </body> </html>