Several common methods of JavaScript arrays

Source: Internet
Author: User

1. Join ()

The Array.join method converts all elements in an array to strings and joins together, returning the last generated string

Example

var a=[1,2,3]

A.join (); ==> "the"

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

The Array.join () method is an inverse operation of the String.Split method.


2, Reverse ()

The Array.reverse () method reverses the order of the 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 vegetarian group and returns the sorted array.


4, Concat ()

The Array.concat () method creates and returns a new array whose elements include the elements of the original array called concat () and each parameter of concat ().

var a=[1,2,3];

A.concat (4,5) //return [1,2,3,4,5]


5, Slice ()

The Array.slice () method returns a fragment or sub-array of the specified array whose two parameters specify the start and end position of the fragment, respectively.

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

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

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

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


6, Splice ()

The Array.splice () method is a common method of inserting or deleting elements in an array, unlike slice () and concat (), which splice () modifies the called Array.

Splice () can remove elements from an array, insert elements into an array, or work with a colleague to do both.

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

A.splice (4); //return [5,6,7,8]; A is [1,2,3,4]

A.splice //Return [2,3];a is [1,4]


7. Push () and pop ()

The push () and Pop () methods allow arrays to be used as stacks.

Stack advanced after you know, do not list


8, Unshift () and Shift ()

The behavior of the Unshift () and Shift () methods is very similar to push () and pop (), unlike the fact that the former is an element in the head of the array rather than the tail.

Insert and delete operations.


9, ToString () and toLocaleString ()

That's not going to explain.







Several common methods of JavaScript arrays

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.