Practical Tips for javascript Array Operations

Source: Internet
Author: User
Tags javascript array

1. concat Method
[Function] concatenates multiple arrays. This method does not change the existing array. It only returns a copy of the combined array.
[Syntax] arrayObj. concat (array1, array2 ,...)
[Instance]

[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
2. join method,
[Function] converts an existing array object into a string, which can be concatenated with a specified separator.
[Syntax] arrayObj. join (separator). The default Delimiter is ",".
[Instance]

[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
3. pop and shift Methods
[Function] The pop method deletes and returns the last element of the array. At the same time, the length of the array changes. In contrast to the shift () method, the first element of the array is deleted and returned.
[Syntax] arrayObj. pop | shift ()
[Instance]

[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
4. push and unshift Methods
[Function] adds one or more elements to the end of the array and returns a new length. At the same time, the length of the array will be changed, which corresponds to the unshift method. Note that the return value of the method is not a new array, but the length of the new array.
[Syntax] arrayObj. push | unshift (ele1, ele2, ele3 ,...)
[Instance]

[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
5. reverse Method
[Function] reverses the order of elements in the array.
[Syntax] arrayObj. reverse ()
[Instance]

[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
6. slice Method
[Function] returns the elements selected from the existing array.
[Syntax] arrayObj. slice (startposition, endposition). The first parameter is required, and the second parameter is optional. If no value is specified, the default value is from startposition to the last element. Note that the element that contains the position startposition does not include the endposition.
[Instance]

[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
7. sort Method
[Function] is used to sort array elements.
[Syntax] arrayObj. sort (sortRule ()). Optional. It indicates the sorting method. Note that sort sorting has several basic principles: first, sort by letter by default, and second, before lowercase letters. In order to sort numbers, you need to write the sorting method by yourself.
[Instance]

[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
8. splice Method
[Function] deletes an array and adds new elements.
[Syntax] arrayObj. splice (index, number, ele1, ele2 ...). The index is the index position and must be a number, indicating the position from which the element is inserted or deleted. Number indicates the number of elements to be deleted from the index position. If it is set to "0", a new element is inserted to the index and the array elements to be deleted are not deleted. (Too mixed ~~~ Directly look at the code instance)
[Instance]

[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
9. toString Method
[Function] The method with very high usage directly converts an array type object to a string type object.
[Syntax] arrayObj. toString ()
[Instance]

[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Note: The new string is composed of the original array, but it is separated by commas (,). To remove the comma (,), you can write it as follows:

[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

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.