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
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 );
["George", "John", "James", "Adrew", "Martin"]
spliceFunction - Replace elements
["George", "John", "James", "Adrew", "Martin"]
arr.splice( arr.indexOf( 'James' ), 1, 'Tom' );
["George", "John", "Tom", "Adrew", "Martin"]
splice function - replace and append Element
["George", "John", "Tom", "Adrew", "Martin"]
arr.splice( arr.indexOf( 'Tom' ), 1, 'Judy', 'Linda', 'Alisa' );
["George", "John", "Judy", "Linda", "Alisa", "Adrew", "Martin"]
splice function - append elements
["George", "John", "Judy", "Linda", "Alisa", "Adrew", "Martin"]
arr.splice( arr.indexOf( 'Linda' ) + 1, 0, 'Bill', 'Blake' );
["George", "John", "Judy", "Linda", "Bill", "Blake", "Alisa", "Adrew", "Martin"]
arr.indexOf( 'Linda' ) + 1 is after the array element Linda
Delete the first element in the array
arr.shift();
["John", "Judy", "Linda", "Bill", "Blake", "Alisa", "Adrew", "Martin"]
Delete the last element in the array
arr.pop();
["John", "Judy", "Linda", "Bill", "Blake", "Alisa", "Adrew"]
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!