Several common methods of javascript array, javascript Array

Source: Internet
Author: User
Tags javascript array

Several common methods of javascript array, javascript Array

1. join ()

The Array. join method converts all elements in the Array into strings and concatenates them. The final generated string is returned.

Example:

Var a = [1, 2, 3]

A. join (); => "1, 2, 3"

A. join ("") => "1 2 3"

The Array. join () method is the reverse operation of the String. split method.


2. reverse ()

The Array. reverse () method reverses the order of elements in the Array.

Var a = [1, 2, 3]

A. reverse (). join () => "3, 2, 1"


3. sort ()

The Array. sort () method sorts the elements in the element group and returns the sorted Array.


4. concat ()

The Array. concat () method is created and a new Array is returned. Its elements include the elements of the original Array that calls concat () and every parameter of concat.

Var a = [1, 2, 3];

A. concat () // return [, 5]


5. slice ()

The Array. slice () method returns a fragment or sub-Array of the specified Array. Its Two Parameters specify the start and end positions of the fragment respectively.

Var a = [1, 2, 3, 4, 5]

A. slice () // return [, 3]

A. slice (3) // return [4, 5]

A. slice (1,-1) // return [2, 3, 4]


6. splice ()

The Array. splice () method inserts or deletes elements in an Array. Unlike slice () and concat (), splice () modifies the called Array.

Splice () can delete elements from the array, insert elements into the array, or colleagues to complete these two operations.

Var a = [1, 2, 3, 4, 5, 6, 7, 8]

A. splice (4); // return [,]; a is [,]

A. splice () // Returns [2, 3]; a is []


7. push () and pop ()

The push () and pop () methods allow the use of arrays as stacks.

After the stack is advanced, you will understand it and will not list it.


8. unshift () and shift ()

The behavior of the unshift () and shift () methods is very similar to push () and pop (). The difference is that the former is element in the array header rather than the tail.

Insert and delete operations.


9. toString () and toLocaleString ()

This is not explained.







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.