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

Delete specified elements from JS array

php中世界最好的语言
Release: 2018-04-18 13:39:03
Original
3484 people have browsed it

This time I will bring you JSarrayDeleteSpecify the element, JS array delete the specified elementWhat are the precautions, the following is a practical case, one Get up and take a look.

In everyone's use of JavaScript, deleting specific elements from arrays has always been a problem for many people. How to delete specific elements from JavaScript arrays? The following article will give you a detailed introduction.

Source array

var arr = ["George", "John", "Thomas", "James", "Adrew", "Martin"];
Copy after login

Pseudo delete>

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

However, please note that this means that the length of the array Array, that is, the variable arr, remains unchanged

Completely delete

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. Fulfill this need! When it comes to 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, and 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 replacement If deleteCount is 1 and more than one parameter value is given in the items parameter position, it means replacing and appending elements
Use the splice method to delete the element value left by the above pseudo-deletion 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’s talk about its other functions, such as replacing elements, appending elements, etc.!

splice function - replace element

Now the array structure is like this:

["George", "John", "James", "Adrew", "Martin"]
Copy after login
Copy after login
Want to replace 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 - replaces and appends elements

Now the current array structure is like this:

["George", "John", "Tom", "Adrew", "Martin"]
Copy after login
Copy after login
Want to replace 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 looks like this:

["George", "John", "Judy", "Linda", "Alisa", "Adrew", "Martin"]
Copy after login
Copy after login
splice function - append element

You can choose any position to append elements, 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, add Bill and Blake

arr.splice( arr.indexOf( 'Linda' ) + 1, 0, 'Bill', 'Blake' );
Copy after login
between Linda and Alisa The appended array structure looks like this:

["George", "John", "Judy", "Linda", "Bill", "Blake", "Alisa", "Adrew", "Martin"]
Copy after login
  • The 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. That is to say, the elements are not deleted.
  • 'Bill', 'Blake' This is the last parameter items of the splice method of the built-in object Array. It means 0 and more, according to deleteCount Different parameter values ​​have different meanings. Here the deleteCount parameter is 0 and items has two values ​​to represent this parameter. In this case, the element values ​​'Bill', 'Blake'
  • are appended.
The above is about deleting specific elements in the array. It is too simple to delete the first element and the last element. I will briefly mention it here

Delete the first element in the array

arr.shift();
Copy after login
The deleted array 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
The deleted array looks like this:

["John", "Judy", "Linda", "Bill", "Blake", "Alisa", "Adrew"]
Copy after login
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

Detailed steps for using Django multiple databases

node.js implements the encapsulation of WeChat interface

The above is the detailed content of Delete specified elements from JS array. 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!