JS Array to delete elements based on subscript

Source: Internet
Author: User
Tags array sort array to string

have been using JS related things, but rarely summarized, today encountered the operation JS array of some problems, the JS array has a further understanding!

1. Creating an array
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 a value

2, Value, assignment
var item = Array[index];//Gets the value of the specified element
Array[index] = value;//Assign a value to the specified element

3. Add new elements
Array.push (item1,item2......itemn);//Add one or more elements to the array, returning the length of the new array
Array.unshift (item1,item2......itemn);//Adds one or more elements to the beginning of the array, the original element position is automatically moved back , and the length of the new array is returned
Array.Splice (Start,delcount,Item1,item2......itemn); //Remove Delco from start position unt an element and then inserts one or more new elements from start position

4. Deleting elements
Array.pop ();//Delete the last element and return the element
Array.shift ();//Delete the first element, the position of the array element is automatically moved forward, and the deleted element is returned
Array.Splice (Start,delcount); //Remove Delco from start position Unt an element

5. Merging and intercepting arrays
Array.slice (start,end);//Returns the part of an array as an array, noting that the element of end is not included , If the end is omitted All elements after start are copied
Array.concat (array1,array2); //stitching multiple arrays into an array

6.Sorting of arrays
Array.Reverse (); //Array inversion
Array.sort (); //array sort, return array address

7. Array to String
Array.Join (separator); //Array reason is used separator connected.

The list of all the ways to delete an array element is not found! So I looked up some information and found a solution.
Deleting an array element requires extending the array prototype prototype.

array.prototype.del=function (index) {
       < /c13> if (IsNaN (index) | | Index>=this.length) {
       return false;
        }
       < /c12> for (Var i=0,n=0;i
       if (This[i]!=this[index]) {
            < /c55> this[n++]=this[i] ;
       < /c12> }
        }
       this.length-=1;
    };

JS Array to delete elements based on subscript

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.