Practical Tips for javascript Array Operations _ javascript skills

Source: Internet
Author: User
Tags javascript array
The methods of arrays and string objects are very confusing. Therefore, you can list them so that you do not need to query them each time. 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]

<Script type = "text/javascript"> var array1 = new Array ("1", "2"); var array2 = new Array ("3", "4 "); var array3 = array1.concat (array2); document. write (array3 + ""); document. write (array3 [0]); script
[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]

<Script type = "text/javascript"> var array1 = new Array ("1", "2"); document. write (array1.join (); script
[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]

<Script type = "text/javascript"> var array1 = new Array ("1", "2", "3", "4"); document. write (array1.pop () + "<br>"); document. write (array1.length + "<br>"); document. write (array1.shift () + "<br>"); document. write (array1.length); script
[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]

<Script type = "text/javascript"> var array1 = new Array ("1", "2", "3", "4 "); var newL = array1.push ("5", "6"); document. write (newL + "<br>"); document. write (array1); script
[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]

<Script type = "text/javascript"> var array1 = new Array ("1", "2", "3", "4"); document. write (array1.reverse (); script
[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]

<Script type = "text/javascript"> var array1 = new Array ("1", "2", "3", "4"); document. write (array1.slice (0, 2); script
[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]

<Script type = "text/javascript"> var array1 = new Array ("c", "a", "d", "B "); var array2 = new Array ("c", "a", "D", "B"); var array3 = new Array ("100", "2 ", "3", "1") function sortRule (a, B) {return a-B;} document. write (array1.sort () + "<br>"); document. write (array2.sort () + "<br>"); document. write (array3.sort (sortRule); script
[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]

<Script type = "text/javascript"> var array1 = new Array ("c", "a", "d", "B"); array1.splice (0, 0, "m") document. write (array1 + "<br>"); array1.splice (0, 2, "n") document. write (array1); script
[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]

<Script type = "text/javascript"> var array1 = new Array ("c", "a", "d", "B"); strObj = array1.toString (); document. write (strObj + "<br>"); script
[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:

<Script type = "text/javascript"> var array1 = new Array ("c", "a", "d", "B"); strObj = array1.toString (). replace (/,/g, ""); document. write (strObj. replace + "<br>"); script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

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.