Home > Web Front-end > JS Tutorial > body text

How to delete elements at specified positions in js array? 2 ways to delete elements at specified positions

青灯夜游
Release: 2020-07-10 17:23:31
Original
113431 people have browsed it

The content of this article is to introduce the method of deleting elements at specified positions in js arrays (one-dimensional), so that everyone can understand how to delete elements at specified positions in js arrays. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Recommended ManualJavaScript Chinese Reference Manual

How to delete elements from js array? In the previous article [How to use pop() and shift() in js to delete array elements? ] We introduced the method of deleting the first element and the last element in a js one-dimensional array. You can take a look if you need.

Let’s introduce in detailWhat are the two methods of deleting elements at specified positions in js one-dimensional arrays? how to use?

Method 1: The js splice() method deletes the array element at the specified position

The splice() method can remove one or more js array elements from the specified position Delete at the position and then return the deleted array element.

Grammar:

数组.splice(index,howmany);
Copy after login

index: Indicates deleting elements from the specified position (where);

howmany: Indicates how many elements should be deleted. Assigning a value of 0 means not Delete elements;

Code example: delete the second and third elements of the animal array (the two consecutive elements after the first item)

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
	</head>
	<body>
		<div class="demo">
			<p>数组:cat,elephant,tiger,rabbit;<br>数组长度为:4</p>
		    <button onclick="myFunction()">点我--shift()删除元素</button>
		</div>
	</body>
	<script type="text/javascript">
		function myFunction(){
		   var animal = ["cat", "elephant", "tiger","rabbit"];
		   document.write("<p>数组:"+animal+"<br>数组长度:"+ animal.length+"</p>");
		   var animal1= animal.splice(1,2);
		   document.write("<p>新数组:"+animal+"<br>删除的元素为:"+ animal1+"<br>数组长度:"+ animal.length+"</p>");
		}
    </script>
</html>
Copy after login

Rendering:

How to delete elements at specified positions in js array? 2 ways to delete elements at specified positions

Recommended related articles: ​ ​
1.How to remove specified elements from an array in js (two methods)
2.Delete specified elements from JS array       
Related video tutorials:

1.JavaScript Quick Start_Jade Girl Heart Sutra Series

Method 2: js delete method to delete the array element at the specified position

delete method You can delete an element in the js array from a specified position through the subscript of the array. After deleting the element in the array, the value of the subscript will be set to undefined, and the length of the array will not change.

Syntax:

delete.数组[数组下标];/*数组下标控制指定位置,下标从0开始*/
Copy after login

Code example: Delete the second element of the animal array, that is: animal[1], and then the value of animal[1] will be: undefined.

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
	</head>
	<body>
		<div class="demo">
			<p>数组:cat,elephant,tiger,rabbit;<br>数组长度为:4</p>
		    <button onclick="myFunction()">点我--delete删除元素</button>
		</div>
	</body>
	<script type="text/javascript">
		function myFunction(){
		   var animal = ["cat", "elephant", "tiger","rabbit"];
		   document.write("<p>数组:"+animal+"<br>数组长度:"+ animal.length+"</p>");
		   var animal1= delete animal[1];
		   document.write("<p>新数组:"+animal+"<br>删除元素后,现在为:"+ animal[1]+"<br>数组长度:"+ animal.length+"</p>");
		}
    </script>
</html>
Copy after login

Rendering:

How to delete elements at specified positions in js array? 2 ways to delete elements at specified positions

Summary: The above are the two methods of adding elements to js arrays introduced in this article, respectively. For the splice() method and delete method. Which method to choose at work depends on work needs and personal habits. You can try it yourself to deepen your understanding. I hope this article can help you!

For more related tutorials, please visit: JavaScript video tutorial, jQuery video tutorial, bootstrap video tutorial!

The above is the detailed content of How to delete elements at specified positions in js array? 2 ways to delete elements at specified positions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!