Home  >  Article  >  Web Front-end  >  Introduction to methods of deleting specific elements from JavaScript arrays

Introduction to methods of deleting specific elements from JavaScript arrays

巴扎黑
巴扎黑Original
2017-09-07 10:23:241421browse

Deleting specified elements from js arrays is a problem that each of us encounters. There is a lot of information on this aspect on the Internet, but some of it is too old and some of the content is not comprehensive enough, so I will sort it out myself. This article 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 delete

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 the length of the array Array, that is, the variable arr, remains unchanged


Complete deletion

What is complete deletion? You may also think of this question literally, it is really deleting the array. The element value of Array will change the length of the array. This requirement can be achieved through the splice method of the built-in array object Array! Speaking of the splice method, we need to 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: the 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 );

The deleted array looks like this:



["George", "John", "James", "Adrew", "Martin"]

Now that it’s said By the way, the splice method will talk about its other functions, such as replacing elements, appending elements, etc.!


splice function - replace elements

Now the array structure is like this:



["George", "John", "James", "Adrew", "Martin"]

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


Now the current array structure is like this:


["George", "John", "Tom", "Adrew", "Martin"]

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 after replacement and appending The structure looks like this:


["George", "John", "Judy", "Linda", "Alisa", "Adrew", "Martin"]

splice function - append elements


You can choose to append elements Any position depends 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"]

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


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

The appended array structure looks like this:


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

    Starting position
  • arr .indexOf( 'Linda' ) + 1

    is after the array element Linda

  • The number parameter of deleted elements is set here to 0. This is the key to appending elements, that is Say not to delete elements
  • 'Bill', 'Blake' This is the last parameter items of the splice method of the built-in object Array. It means 0 or more, according to the deleteCount parameter value Different representations have different meanings. The deleteCount parameter here is 0 and items has two values ​​​​to represent this parameter. The example is to append the element values ​​'Bill', 'Blake'

  • 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();

The deleted array looks like this:


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


Delete the last element in the array


arr.pop();

The deleted array looks like this:


["John", "Judy", "Linda", "Bill", "Blake", "Alisa", "Adrew"]

The above is the detailed content of Introduction to methods of deleting specific elements from JavaScript arrays. For more information, please follow other related articles on the PHP Chinese website!

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