Home Web Front-end JS Tutorial JS delete an element in the array instance code

JS delete an element in the array instance code

Feb 05, 2018 am 09:56 AM
javascript element Example

This article mainly shares with you a JS method to delete an element in an array. It has a good reference value and I hope it will be helpful to everyone. Let’s follow the editor to take a look, I hope it can help everyone.

Delete an element specified in the array

First, you can define a function for the JS array object to find the position of the specified element in the array. , that is, the index, the code is:


Array.prototype.indexOf = function(val) { 
for (var i = 0; i < this.length; i++) { 
if (this[i] == val) return i; 
} 
return -1; 
};
Copy after login

Then use the index to get the element, and use the js array's own inherent function to delete the element:

The code is:


Array.prototype.remove = function(val) { 
var index = this.indexOf(val); 
if (index > -1) { 
this.splice(index, 1); 
} 
};
Copy after login

In this way, such a function is constructed. For example, I have an array:


var emp = [&#39;abs&#39;,&#39;dsf&#39;,&#39;sdf&#39;,&#39;fd&#39;]
Copy after login

If we want to delete 'fd', we can use:


emp.remove(&#39;fd&#39;);
Copy after login

Delete an item of the array

splice(index,len,[item]) Note: This method will change the original array.

splice has 3 parameters, it can also be used to replace/delete/add one or several values ​​in the array

index: array starting subscript len: replacement/delete length item :Replacement value, if the delete operation is performed, the item will be empty

For example: arr = ['a','b','c','d']

Delete


//删除起始下标为1,长度为1的一个值(len设置1,如果为0,则数组不变) 
var arr = [&#39;a&#39;,&#39;b&#39;,&#39;c&#39;,&#39;d&#39;]; 
arr.splice(1,1); 
console.log(arr); 
//[&#39;a&#39;,&#39;c&#39;,&#39;d&#39;]; 
 
 
//删除起始下标为1,长度为2的一个值(len设置2) 
var arr2 = [&#39;a&#39;,&#39;b&#39;,&#39;c&#39;,&#39;d&#39;] 
arr2.splice(1,2); 
console.log(arr2); 
//[&#39;a&#39;,&#39;d&#39;]
Copy after login

Replace


##

//替换起始下标为1,长度为1的一个值为‘ttt&#39;,len设置的1 
var arr = [&#39;a&#39;,&#39;b&#39;,&#39;c&#39;,&#39;d&#39;]; 
arr.splice(1,1,&#39;ttt&#39;); 
console.log(arr);   
//[&#39;a&#39;,&#39;ttt&#39;,&#39;c&#39;,&#39;d&#39;] 
 
 
var arr2 = [&#39;a&#39;,&#39;b&#39;,&#39;c&#39;,&#39;d&#39;]; 
arr2.splice(1,2,&#39;ttt&#39;); 
console.log(arr2);   
//[&#39;a&#39;,&#39;ttt&#39;,&#39;d&#39;] 替换起始下标为1,长度为2的两个值为‘ttt&#39;,len设置的1
Copy after login

Add ---- len setting is 0, item is the added value

##

var arr = [&#39;a&#39;,&#39;b&#39;,&#39;c&#39;,&#39;d&#39;]; 
arr.splice(1,0,&#39;ttt&#39;); 
console.log(arr);   
//[&#39;a&#39;,&#39;ttt&#39;,&#39;b&#39;,&#39;c&#39;,&#39;d&#39;] 表示在下标为1处添加一项&#39;ttt&#39;<span style="font-size:14px;font-family:Arial, Helvetica, sans-serif;background-color:rgb(255,255,255);"> </span>
Copy after login

After the delete method deletes the elements in the array, it will set the subscripted value to undefined, and the array The length will not change

var arr = [&#39;a&#39;,&#39;b&#39;,&#39;c&#39;,&#39;d&#39;]; 
delete arr[1]; 
arr; 
//["a", undefined × 1, "c", "d"] 中间出现两个逗号,数组长度不变,有一项为undefined
Copy after login

Related recommendations:

PHP example code for deleting specified subscript elements in an array

5 JavaScript algorithms for deleting duplicate elements from an array

How to delete null elements from an array using PHP recursion

The above is the detailed content of JS delete an element in the array instance code. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Learn best practice examples of pointer conversion in Golang Learn best practice examples of pointer conversion in Golang Feb 24, 2024 pm 03:51 PM

Golang is a powerful and efficient programming language that can be used to develop various applications and services. In Golang, pointers are a very important concept, which can help us operate data more flexibly and efficiently. Pointer conversion refers to the process of pointer operations between different types. This article will use specific examples to learn the best practices of pointer conversion in Golang. 1. Basic concepts In Golang, each variable has an address, and the address is the location of the variable in memory.

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

The relationship between the number of Oracle instances and database performance The relationship between the number of Oracle instances and database performance Mar 08, 2024 am 09:27 AM

The relationship between the number of Oracle instances and database performance Oracle database is one of the well-known relational database management systems in the industry and is widely used in enterprise-level data storage and management. In Oracle database, instance is a very important concept. Instance refers to the running environment of Oracle database in memory. Each instance has an independent memory structure and background process, which is used to process user requests and manage database operations. The number of instances has an important impact on the performance and stability of Oracle database.

How to check if an element contains an attribute value in jQuery? How to check if an element contains an attribute value in jQuery? Feb 28, 2024 pm 02:54 PM

In jQuery, we often need to check whether an element contains a specific attribute value. Doing this helps us perform actions based on the attribute values ​​on the element. In this article, I will introduce how to use jQuery to check whether an element contains a certain attribute value, and provide specific code examples. First, let's take a look at some common methods in jQuery to operate the attributes of elements: .attr(): used to get or set the attribute value of an element. .prop(): used to get or set the attribute value of an element

How to implement an online electronic signature system using WebSocket and JavaScript How to implement an online electronic signature system using WebSocket and JavaScript Dec 18, 2023 pm 03:09 PM

Overview of how to use WebSocket and JavaScript to implement an online electronic signature system: With the advent of the digital age, electronic signatures are widely used in various industries to replace traditional paper signatures. As a full-duplex communication protocol, WebSocket can perform real-time two-way data transmission with the server. Combined with JavaScript, an online electronic signature system can be implemented. This article will introduce how to use WebSocket and JavaScript to develop a simple online

Implement jQuery method to dynamically add elements to div Implement jQuery method to dynamically add elements to div Feb 24, 2024 pm 08:03 PM

jQuery is a popular JavaScript library used to simplify JavaScript programming. It provides a rich set of features and methods, including the ability to dynamically add elements to HTML elements. This article will introduce how to use jQuery to dynamically add elements to divs, and provide specific code examples. First, we need to introduce the jQuery library into the HTML document. It can be introduced in the following ways:

Use jQuery to determine the display state of elements Use jQuery to determine the display state of elements Feb 23, 2024 pm 09:24 PM

Use jQuery to determine whether an element is visible or not. In web development, we often encounter situations where we need to determine whether an element is visible. Judging and operating the visibility of elements can be easily achieved through jQuery. This article will introduce how to use jQuery to determine the visibility of elements, and provide specific code examples. jQuery's element visibility judgment methods In jQuery, you can use some specific methods to judge the visibility of elements. The most commonly used method is .is(':vis

See all articles