Home >Web Front-end >Front-end Q&A >How to delete an array using splice in jquery
In jquery, you can use the "array.splice(index,howmany)" method to delete an array; the parameter index is used to specify the position from which to delete elements, and the parameter howmany is used to specify how many elements should be deleted. If it is not set This parameter will delete all elements from index to the end of the array.
The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.
The splice() method is used to add or delete elements in an array.
This method will change the original array.
If an element is deleted, an array of elements is returned. If no elements were removed, an empty array is returned.
The syntax is:
array.splice(index,howmany,item1,.....,itemX)
index Required. Specifies where to add/remove elements. This parameter is the index of the array element to start inserting and/or deleting, and must be a number.
howmany Optional. Specifies how many elements should be removed. Must be a number, but can be "0". If this parameter is not specified, all elements starting from index to the end of the original array are deleted.
item1, ..., itemX Optional. An example of a new element to be added to the array
is as follows:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>123</title> </head> <body> <p id="demo">点击按钮添加和删除元素。</p> <button onclick="myFunction()">点我</button> <script> function myFunction(){ var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.splice(2,1,"Lemon","Kiwi"); var x=document.getElementById("demo"); x.innerHTML=fruits; } </script> </body> </html>
Output result:
Related video tutorial recommendation: jQuery Video tutorial
The above is the detailed content of How to delete an array using splice in jquery. For more information, please follow other related articles on the PHP Chinese website!