Basic usage of JS array and array removing elements based on subscript (numeric or character) _javascript tips

Source: Internet
Author: User
Tags array sort
1. Create an array
Copy Code code 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 a value

2, take value, assign value
Copy Code code as follows:

var item = array[index];//Gets the value of the specified element
Array[index] = value;//Assign value to the specified element

3. Add new elements
Copy Code code as follows:

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 delcount elements from start position, then insert one or more new elements from start position

4, delete elements
Copy Code code as follows:

Array.pop ()//deletes the last element and returns the element
Array.shift ()//deletes the first element, the array element position is automatically moved forward, and the deleted element is returned
Array.splice (Start,delcount);//Remove delcount elements from start position

5, the integration of the array, interception
Copy Code code as follows:

Array.slice (start,end);//Returns a portion of an array as an array, noting that the end-corresponding element is not included, if the end will copy all elements after start
Array.concat (array1,array2);//to stitch multiple arrays into an array

6, sorting the array
Copy Code code as follows:

Array.reverse ();//Array inversion
Array.Sort ();//array sort, return array address

7, the array of the string
Copy Code code as follows:

Array.join (separator);//connect array reason with separator

That's all there is. method for deleting an array element based on a subscript! So I looked up some information and found a solution.
Removing an array element requires extending the array prototype prototype.
The subscript of a general array is a numeric type, but there are also character subscript
Numerical processing, first of all, the following code to write out, is the expansion of the array of
Copy Code code as follows:

Array.prototype.del = function (dx)
{
if (isNaN (dx) | | Dx>this.length) {return false;}
This.splice (dx,1);
}

Secondly, the numerical value of the direct transfer of the parameters can be. For example var arr = ["AA", "BB"];arr.del (0);
Let's say the subscript for character type
Copy Code code as follows:

var arr = [].
arr["AA"] = 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.