JavaScript array definition and digital manipulation techniques _javascript skills

Source: Internet
Author: User
Tags array definition array length javascript array

I. Introduction to Arrays

The element types in an array can be numeric, string, Boolean, or even an array.

Two, the definition array

1, through the constructor of the array to define the array:

var arr=new Array ();
var arr=new Array (size);
var arr=new Array (Element1,element2,...);

  2, directly define the array:

var arr=["string", true,13];

PS: Like object, this method does not call the array () constructor.

Three, array elements

1, access to array elements: through the access operator [] such as: arr[2];

2. Add array elements: Assign values directly to arrays of elements.

3. Delete array elements:

Once an array element is defined, it cannot be deleted. Use the delete operator to delete only the value of an array element, making the element value undefined, but not deleting an array element and not reducing the elements in the array.

4, the number of array elements: array length. Array.Length

Four, the method of the array

ToString (): Converts an array to a string

Join (): Concatenate array elements into strings and automatically concatenate array elements with commas. 1,2,3

Join (STR): Concatenate array elements into strings, using STR as a connector to concatenate array elements. Join (-): 1-2-3

 Stack method:

Push (): Adds an element at the end of the array that returns the length of the array after the element is added.

Pop (): Removes the last element from the end of the array, reduces the length value of the array, and then returns the removed element.

 Queue method: 

Push (): Adds an element at the end of the array that returns the length of the array after the element is added.

Shift (): Removes an element from the front end of the array, reduces the length of the array, and then returns the removed element. And move all remaining elements forward 1 digits.

Unshift (): Adds an element from the front end of the array that returns the length of the array after the element was added (ie does not support the return value and returns undefined). An

  Sort:

Reverse (): Reverse

Sort (): Order by character encoding

Sort: The order argument must be a function that should have two parameters.

Positive order sort
function Ascorder (value1, value2) { 
if (value1 < value2) { 
return-1;
} else if (Value1 > Val Ue2) {return 
1;
} else {return 
0;
}
Reverse order
function Descorder (value1, value2) { 
if (value1 < value2) {return 
1;
} else if (value1 > value2) { 
return-1
} else {return 
0;
}
var box = [0,1,5,10,15]; 
Box.sort (Ascorder); 
Box.sort (Descorder); 

 Action method:

Concat (): Adds elements and generates new arrays, the original array length does not change.

Splice (): Deletes, replaces, or inserts an array element.

Splice (Start,count,value,...): start: Starting position; count: Number of elements

Slice (Start,end): Returns a portion of the array.

Tolocalstring (): Convert to local string.

The above is a small set of JavaScript to introduce the definition of the array and digital operation skills related knowledge, hope to help you!

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.