Home > Article > Web Front-end > Delete specified elements from JS array
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"];
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"]
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 arrayArray.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, and the meaning of its parameters is:
index value
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’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"]Want to replace 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 - replaces and appends elements Now the current array structure is like this:
["George", "John", "Tom", "Adrew", "Martin"]Want to replace 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 looks like this:
["George", "John", "Judy", "Linda", "Alisa", "Adrew", "Martin"]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"]For example, add Bill and Blake
arr.splice( arr.indexOf( 'Linda' ) + 1, 0, 'Bill', 'Blake' );between Linda and Alisa The appended array structure looks like this:
["George", "John", "Judy", "Linda", "Bill", "Blake", "Alisa", "Adrew", "Martin"]
arr.indexOf( 'Linda' ) 1 is after the array element Linda
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"]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!