Home >Web Front-end >JS Tutorial >How to remove an element from an array in jquery
Method: 1. Use the each() method to loop through the array; 2. In the loop statement, use the if statement to determine whether the array element is the specified value; 3. Use the splice() method to remove the specified array Element, the syntax is "array element.splice(array index value, 1);".
The operating environment of this tutorial: windows7 system, jquery1.10.0 version, Dell G3 computer.
How to remove an element in an array with jquery
1. Create a new html file named test.html for explaining jquery How to delete an element from an array. Only after introducing the jquery.min.js library file and successfully loading the file can you use the methods in jquery. Use "[]" to create a js array and save it in the arr variable. Use the each() method to traverse the array, and execute the function method each time it traverses the array elements. One is the index parameter (array index value), and the other is the item parameter (the array element corresponding to the index).
2. In the function method, use the if statement to determine the element to be deleted. For example, to delete the c element in the array, if the if condition is true, use splice () method deletes the specified array element through index index. Finally, use the alert() method to output the array after deleting the specified element.
Open the test.html file in the browser to view the results.
Summary:
1. Create a test.html file.
2. In the js tag, use "[]" to create a js array.
3. In the js tag, use the each() method to traverse the array. Each time you traverse the array elements, use the if statement to determine the element to be deleted. For example, to delete the c element in the array, if If the condition is true, use the splice() method to delete the specified array element through index index.
Note:
The splice() method adds/removes items to/from the array, and then returns the deleted item.
Recommended related video tutorials: jQuery video tutorial
The above is the detailed content of How to remove an element from an array in jquery. For more information, please follow other related articles on the PHP Chinese website!