Go: JavaScript removes an element from an array of specified values (not a specified position)

Source: Internet
Author: User

For example, array {1,2,3,4,5}

To delete the 3 inside the array to get {1,2,4,5}

<script type= "Text/javascript" >Array.prototype.indexOf=function(Val) {//prototype adding properties to an array             for(vari = 0; I < This. length; i++) {//This is the number of array class elements that refer to an array, this.length                if( This[I] = = val)returnI//The elements in the array are equal to the parameters passed in, I is the subscript, and if present, I return            }            return-1;        }; Array.prototype.remove=function(Val) {//prototype adding properties to an array            varindex = This. IndexOf (Val);//call the index () function to get the return value of the lookup            if(Index >-1) {                 This. Splice (index, 1);//Use the splice () function to delete a specified element, the splice () method for inserting, deleting, or replacing an element of an array            }        }; varArray = [1, 2, 3, 4, 5]; Array.remove (3);</script>

A useful code

Array.prototype.indexOf =function(val) { for(vari = 0; I < This. length; i++) {                if( This[I] = = val)returni; }            return-1; }; Array.prototype.remove=function(val) {varindex = This. IndexOf (Val); if(Index >-1) {                 This. Splice (Index, 1); }        };

For an array that needs to be deleted, reference Array.remove (val); The function can be an array of the names of the deleted arrays Val is the specific content in the specified deleted array

Go: JavaScript removes an element from an array of specified values (not a specified position)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.