JS delete an element in the array instance code
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; };
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); } };
In this way, such a function is constructed. For example, I have an array:
var emp = ['abs','dsf','sdf','fd']
If we want to delete 'fd', we can use:
emp.remove('fd');
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 = ['a','b','c','d']; arr.splice(1,1); console.log(arr); //['a','c','d']; //删除起始下标为1,长度为2的一个值(len设置2) var arr2 = ['a','b','c','d'] arr2.splice(1,2); console.log(arr2); //['a','d']
Replace
##
//替换起始下标为1,长度为1的一个值为‘ttt',len设置的1 var arr = ['a','b','c','d']; arr.splice(1,1,'ttt'); console.log(arr); //['a','ttt','c','d'] var arr2 = ['a','b','c','d']; arr2.splice(1,2,'ttt'); console.log(arr2); //['a','ttt','d'] 替换起始下标为1,长度为2的两个值为‘ttt',len设置的1
Add ---- len setting is 0, item is the added value
##
var arr = ['a','b','c','d']; arr.splice(1,0,'ttt'); console.log(arr); //['a','ttt','b','c','d'] 表示在下标为1处添加一项'ttt'<span style="font-size:14px;font-family:Arial, Helvetica, sans-serif;background-color:rgb(255,255,255);"> </span>
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 = ['a','b','c','d']; delete arr[1]; arr; //["a", undefined × 1, "c", "d"] 中间出现两个逗号,数组长度不变,有一项为undefined
Related recommendations:PHP example code for deleting specified subscript elements in an array5 JavaScript algorithms for deleting duplicate elements from an arrayHow 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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.

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 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.

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

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

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 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
