Tips for deleting element _ javascript based on subscript in js array

Source: Internet
Author: User
I have been using js-related things, but I have seldom summarized them. today I have encountered some problems in operating js arrays and have a better understanding of js arrays. 1. create an array

The code is as follows:


Var array = new Array ();
Var array = new Array (size); // specify the length of the array
Var array = new Array (item1, item2 ...... ItemN); // create an array and assign values

2. value and value assignment

The code is as follows:


Var item = array [index]; // obtain the value of a specified element
Array [index] = value; // assign a value to a specified element

3. add new elements

The code is as follows:


Array. push (item1, item2 ...... ItemN); // add one or more elements to the array and return the length of the new array.
Array. unshift (item1, item2 ...... ItemN); // add one or more elements to the starting position of the array. The original element position is automatically removed and the length of the new array is returned.
Array. splice (start, delCount, item1, item2 ...... ItemN); // delete delCount elements from the start position and insert one or more new elements from the start position.

4. delete elements

The code is as follows:


Array. pop (); // delete the last element and return this element.
Array. shift (); // delete the first element. the array element is automatically moved forward and the deleted element is returned.
Array. splice (start, delCount); // delete delCount elements from the start position.

5. Merge and intercept arrays

The code is as follows:


Array. slice (start, end );
// Return part of the array in the form of an array. Note that the end element is not included. if the end element is omitted, all elements after start will be copied.
Array. concat (array1, array2 );
// Concatenates multiple arrays into an array

6. sort arrays

The code is as follows:


Array. reverse (); // array inversion
Array. sort (); // sorts the array and returns the array address.

7. convert an array to a string

Array. join (separator); // connects the array with separator.

The method for deleting array elements is not found if all the columns are listed! So I found some information and found the solution.
To delete an Array element, you need to extend the Array prototype.

Array.prototype.del=function(index){    if(isNaN(index)||index>=this.length){      return false;    }    for(var i=0,n=0;i      if(this[i]!=this[index]){        this[n++]=this[i];      }    }    this.length-=1;  };

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.