


Summary of several methods to delete specific elements from JavaScript arrays
Deleting specified elements from js arrays is a problem that each of us encounters. There is a lot of information on this online, but some are too old and some are not comprehensive enough. So I will organize it myself. This article mainly summarizes and introduces various methods for deleting specific elements in JavaScript arrays. Friends in need can refer to it.
Preface
Maybe when it comes to deleting specific elements of an array, you estimate that there is more than one way to achieve it, so let’s take a look. Check out these methods I’ve summarized, they may be helpful to you! Not much to say, let’s take a look at the detailed introduction.
Source array
var arr = ["George", "John", "Thomas", "James", "Adrew", "Martin"];
Pseudo deletion
What is pseudo-deletion? That is to say, setting the array element value to null;
arr[ arr.indexOf( 'Thomas' ) ] = null;
The deleted array looks like this:
["George", "John", null, "James", "Adrew", "Martin"]
But please note that this means that arrayArraythat is, the length of variable arr remains unchanged
Delete completely
What is complete deletion? You may also think of this question literally, which is to actually delete the element values of the array Array, and will change the length of the array. You can use the splice method of the built-in array object Array. To realize this requirement! Speaking of the splice method, let’s talk about its specific parameters:
Array.prototype.splice = function(start,deleteCount,items) {};
The above is the prototype definition of the splice method of the built-in object Array. The Chinese meaning is: splicing. The meaning of its parameters is:
start: starting point index value
- ##deleteCount: number of elements to be deleted
- items: elements replaced/appended after deletion
When the parameter is not added, it means deleting the element, and it must be combined with the parameter value of deleteCount
If deleteCount is 1 and a parameter value is given in the items parameter position, it means replacing
If deleteCount is 1 and the items parameter position is given to more than one parameter value, it means replacing and appending elements
arr.splice( arr.indexOf( null ), 1 );The deleted array looks like this:
["George", "John", "James", "Adrew", "Martin"]Now that we have mentioned the splice method, let me talk about its other functions, such as replacing elements, appending elements, etc. Do it!
spliceFunction - Replace elements
["George", "John", "James", "Adrew", "Martin"]Want to replace the array element James with Tom
arr.splice( arr.indexOf( 'James' ), 1, 'Tom' );The replaced array structure looks like this:
["George", "John", "Tom", "Adrew", "Martin"]
splice function - replace and append Element
["George", "John", "Tom", "Adrew", "Martin"]I want to replace the array element Tom with Judy and append Linda and Alisa
arr.splice( arr.indexOf( 'Tom' ), 1, 'Judy', 'Linda', 'Alisa' );The array structure after replacement and appending is like this:
["George", "John", "Judy", "Linda", "Alisa", "Adrew", "Martin"]
splice function - append elements
["George", "John", "Judy", "Linda", "Alisa", "Adrew", "Martin"]For example, if you want to add Bill and Blake between Linda and Alisa
arr.splice( arr.indexOf( 'Linda' ) + 1, 0, 'Bill', 'Blake' );The added array structure looks like the following :
["George", "John", "Judy", "Linda", "Bill", "Blake", "Alisa", "Adrew", "Martin"]
- Starting position
arr.indexOf( 'Linda' ) + 1
is after the array element Linda
- The number of deleted elements parameter is set here to 0. This is the key to appending elements, which means that the elements are not deleted.
- 'Bill', 'Blake' This is the built-in The last parameter items of the splice method of the object Array represents 0 or more. The meaning will be different depending on the deleteCount parameter value. Here the deleteCount parameter is 0 and items has two values to represent this parameter, as shown That is to say, append the element value 'Bill', 'Blake'
Delete the first element in the array
arr.shift();The array after deletion looks like this:
["John", "Judy", "Linda", "Bill", "Blake", "Alisa", "Adrew", "Martin"]
Delete the last element in the array
arr.pop();After deletion The array looks like this:
["John", "Judy", "Linda", "Bill", "Blake", "Alisa", "Adrew"]The above is all the content of this article. I hope it can help everyone learn JavaScript! !
Related recommendations:
Detailed explanation of JavaScript module mode
Various writing methods of javaScript encapsulation
JavaScript Detailed explanation of observer pattern examplesThe above is the detailed content of Summary of several methods to delete specific elements from JavaScript arrays. For more information, please follow other related articles on the PHP Chinese website!

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version
Chinese version, very easy to use

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
