Notes. ------Array

Source: Internet
Author: User

Array of methods:

1.

The Jion () method converts all the elements in the array into strings and joins them, returning the last generated string .

The Array.jion () method is the inverse of the String.Split () method, which splits the string into blocks to create an array.

2.

The reverse () method tells the reverse order of the elements in the group and returns the inverse array .

var a=[1,2,3];

A.reverse (). Jion (); Return "3,2,1", and now A is [3,2,1]

3.

The sort () method tells the ordering of the elements in the group and returns the sorted array. When you call sort () with no arguments, the array elements are sorted alphabetically .

4.

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

Similar to the push () method. are all added elements.

The difference between them is:

Push         is the   data that is returned by adding a piece of data to the array
Concat is the concatenation of the string returned by the new array

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

The Intercept is the element that starts with start (including start) and ends with end (not including end).

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

A.slice (0,3);//return [n/a]

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

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

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

6.

The Splice () method is a common method of inserting or deleting elements in an array , returning an inserted array or an array of deletions . . Unlike slice () and concat () and push (), splice () modifies the called Array.

Its first parameter specifies the starting position of the insertion or deletion, and the second parameter specifies the number of elements to remove from the array. If you omit the second argument, all elements from the starting point to the end of the array are deleted.

The splice () method returns an array of deleted elements, or an empty array if the element is not deleted.

Splice () index number is not starting from 0!! Splice () inserts the array itself rather than the elements of the array, as opposed to concat () and push () and slice ().

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

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

A.splice;//return [2,3],a=[1,4]

A.splice;//return [4],a=[1]

——————

Insert: Var a=[1,2,3,4,5];

A.splice (2,0, ' A ', ' B ');//return [],a=[1,2, ' A ', ' B ', 3,4,5]

A.splice (2,2,[1,2],3);//return [' A ', ' B '],a=[1,2,[1,2],3,3,4,5]

7.

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

The push () method adds one or more elements to the end of the array and returns the length of the array .

The pop () method instead, he deletes the last element of the array, reduces the length of the array, and returns the value he deleted .

8.

The behavior of the Unshift () and Shift () methods is very similar to that of push () and pop (), which is the insertion and deletion of elements in the head of the array rather than the tail.

The Unshift () and Shift () methods are inserted and deleted in the header of the array.

unshift () is one or more inserts in the head of an array, and returns the length of the array.

shift () deletes the first element of the array and returns the deleted value .

9.

Arrays have the ToString () method as with other JavaScript objects.

For arrays, this method converts each of its elements into a string.

[1,2,3].tostring ();//Generate ' a '

[' A ', ' B ', ' C '].tostring ();//Generate ' a,b,c '

[1,[2, ' C ']].tostring ();//Generate ' a '

Note that this is the same as the string returned by the Jion () method that does not apply to the parameter invocation.

toLocaleString () is a localized version of the ToString () method, which calls the element's toLocaleString () method to convert each array into a string, and the disease uses a local (and custom implementation) delimiter to concatenate strings and generate the final string.

Notes. ------Array

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.