Home >Web Front-end >Front-end Q&A >How to delete an array using splice in jquery

How to delete an array using splice in jquery

WBOY
WBOYOriginal
2022-05-12 11:34:532315browse

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.

How to delete an array using splice in jquery

The operating environment of this tutorial: windows10 system, jquery3.2.1 version, Dell G3 computer.

How to delete an array using splice in jquery

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.

How to delete an array using splice in jquery

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:

How to delete an array using splice in jquery

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn