JavaScript method for deleting a specified value element from an array, javascript Array
This example describes how JavaScript deletes a specified value element from an array. Share it with you for your reference. The specific analysis is as follows:
The following code uses two methods to delete Array elements. The first is to define a separate function, and the second is to define a removeByValue method for the Array object. The call is very simple.
Define the removeByValue function to delete an element.
Function removeByValue (arr, val) {for (var I = 0; I <arr. length; I ++) {if (arr [I] = val) {arr. splice (I, 1); break ;}} var somearray = ["mon", "tue", "wed", "thur"] removeByValue (somearray, "tue"); // somearray will now have "mon", "wed", "thur"
Adding corresponding methods to the array object makes it easier to call. You can directly call the removeByValue method of the array to delete the specified element.
Array. prototype. removeByValue = function (val) {for (var I = 0; I <this. length; I ++) {if (this [I] = val) {this. splice (I, 1); break ;}} var somearray = ["mon", "tue", "wed", "thur"] somearray. removeByValue ("tue"); // somearray will now have "mon", "wed", "thur"
I hope this article will help you design javascript programs.