Home  >  Article  >  Web Front-end  >  How to delete specified elements in an array in js? (code example)

How to delete specified elements in an array in js? (code example)

青灯夜游
青灯夜游Original
2018-10-30 17:37:443017browse

In the process of learning arrays, array operations are a very important part, so how to delete specified elements from an array? This article will introduce to you how to delete specified elements in an array in js. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

This article will introduce you to two ways to delete specified elements in an array, which are:

1. Define a separate function and use the function to delete specified array elements.

2. A removeByValue method is defined for the Array object. It is very simple to call the method to delete the specified array element.

Below we briefly introduce these two methods of deleting specified elements of an array through simple code examples.

1. Define a separate function removeByValue to delete elements

Code example: delete the "tue" element in the array somearray

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
	</head>

	<body>
		<div class="demo">
			<p>数组:mon, tue, wed, thur</p>
			<p class="p"></p>

		</div>

	</body>

	<script type="text/javascript">
		function removeByValue(arr, val) {
			for(var i = 0; i < arr.length; i++) {
				if(arr[i] == val) {
					arr.splice(i, 1);
					break;
				}
			}
		}
		var somearray = ["mon", "tue", "wed", "thur"]
		removeByValue(somearray, "tue");
		//somearray will now have "mon", "wed", "thur"

		document.write("<p>新数组:" + somearray + "</p>");
	</script>

</html>

Rendering:

How to delete specified elements in an array in js? (code example)

2. Define and call the removeByValue method of the array to delete the specified element

Code example: Delete the array somearray The "wed" element in

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
	</head>
	<body>
		<div class="demo">
			<p>数组:mon, tue, wed, thur</p>
			<p>删除指定元素"tue"后:</p>
		</div>
	</body>
	<script type="text/javascript">
		Array.prototype.removeByValue = function(val) {
			for(var i = 0; i < this.length; i++) {
				if(this[i] == val) {
					this.splice(i, 1);
					break;
				}
			}
		}
		var somearray = ["mon", "tue", "wed", "thur"]
		somearray.removeByValue("wed");
		//somearray will now have "mon", "wed", "thur"

		document.write("<p>新数组:" + somearray + "</p>");
	</script>

</html>

Rendering:

How to delete specified elements in an array in js? (code example)

Summary: The above are the two ways to delete specified elements of an array in js introduced in this article , you can try it yourself and deepen your understanding. I hope it will be helpful to your study.

The above is the detailed content of How to delete specified elements in an array in js? (code example). 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