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

Summary of several methods to delete specific elements from JavaScript arrays

韦小宝
Release: 2018-01-25 10:45:00
Original
1101 people have browsed it


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"];
Copy after login

Pseudo deletion

What is pseudo-deletion? That is to say, setting the array element value to null;

arr[ arr.indexOf( 'Thomas' ) ] = null;
Copy after login

The deleted array looks like this:

["George", "John", null, "James", "Adrew", "Martin"]
Copy after login

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) {};
Copy after login

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

The element value left by the above pseudo-deletion is deleted through the splice method. null


arr.splice( arr.indexOf( null ), 1 );
Copy after login

The deleted array looks like this:


["George", "John", "James", "Adrew", "Martin"]
Copy after login
Copy after login

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

Now the array structure is like this:


["George", "John", "James", "Adrew", "Martin"]
Copy after login
Copy after login

Want to replace the array element James with Tom


arr.splice( arr.indexOf( 'James' ), 1, 'Tom' );
Copy after login

The replaced array structure looks like this:


["George", "John", "Tom", "Adrew", "Martin"]
Copy after login
Copy after login

splice function - replace and append Element

Now the current array structure is like this:


["George", "John", "Tom", "Adrew", "Martin"]
Copy after login
Copy after login

I want to replace the array element Tom with Judy and append Linda and Alisa


arr.splice( arr.indexOf( 'Tom' ), 1, 'Judy', 'Linda', 'Alisa' );
Copy after login

The array structure after replacement and appending is like this:


["George", "John", "Judy", "Linda", "Alisa", "Adrew", "Martin"]
Copy after login
Copy after login

splice function - append elements

Append elements you You can choose any position depending on your specific needs. The key lies in the value index position of start! The current array structure is as follows:


["George", "John", "Judy", "Linda", "Alisa", "Adrew", "Martin"]
Copy after login
Copy after login

For example, if you want to add Bill and Blake between Linda and Alisa


arr.splice( arr.indexOf( 'Linda' ) + 1, 0, 'Bill', 'Blake' );
Copy after login

The added array structure looks like the following :


["George", "John", "Judy", "Linda", "Bill", "Blake", "Alisa", "Adrew", "Martin"]
Copy after login

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


#The above is to delete specific elements in the array, then delete the first element and the last element to achieve It’s too simple now, just mention it here briefly


Delete the first element in the array

arr.shift();
Copy after login

The array after deletion looks like this:


["John", "Judy", "Linda", "Bill", "Blake", "Alisa", "Adrew", "Martin"]
Copy after login

Delete the last element in the array

arr.pop();
Copy after login

After deletion The array looks like this:


["John", "Judy", "Linda", "Bill", "Blake", "Alisa", "Adrew"]
Copy after login
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 examples

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

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!