JavaScript array operations Practical tips _javascript Tips

Source: Internet
Author: User
Tags javascript array

1, concat method
[Action] combines multiple arrays, and this method does not change the existing array, it only returns a copy of the associative 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 All SELECT Note: If the need to introduce external JS need to refresh to perform]

2, Join method,
[Effect] Converts an existing array object to 1 strings, which can be concatenated with the specified delimiter.
[Syntax] Arrayobj.join (separator), separator is a separator, and defaults to ",".
Instance
<script type= "Text/javascript" > var array1 = new Array ("1", "2"); document.write (Array1.join ()); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

3, Pop, shift method
The action Pop method deletes and returns the last element in the array, while the length of the array changes. The opposite is the shift () method, which deletes and returns the first element of the array.
[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 All SELECT Note: If the need to introduce external JS need to refresh to perform]

4, Push, Unshift method
Action 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, corresponding to the Unshift method. Note that the return value of the method is not the 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 All SELECT Note: If the need to introduce external JS need to refresh to perform]

5, Reverse method
[Action] 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 All SELECT Note: If the need to introduce external JS need to refresh to perform]

6, Slice method
function to return the elements of a selection in an existing array.
[Syntax] Arrayobj.slice (startposition,endposition), the first argument is required, the second is optional, if not write, the default is from StartPosition to the last element. Note that the element containing the startposition is included, not the endposition.
Instance
<script type= "Text/javascript" > var array1 = new Array ("1", "2", "3", "4"); document.write (Array1.slice (0,2)); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

7. Sort method
Action is used to sort the array elements.
[Syntax] Arrayobj.sort (Sortrule ()). parameter is optional, representing the method of sorting. Specifically, sort sort has several basic principles, first, by default alphabetical order; second, uppercase letters precede lowercase letters. Base with this, for numeric sorting, you need to write your own sorting method.
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 ("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 All SELECT Note: If the need to introduce external JS need to refresh to perform]

8, Splice method
[Action] Deletes the array and joins the new element.
[Syntax] Arrayobj.splice (Index,number,ele1,ele2 ...). Index is an indexed position and must be a number representing the location from which to insert or delete the element. Number that represents the count of elements removed from the index position, or "0", which means inserting a new element at index, and not deleting the later array elements. (speak too mixed ~ ~ ~ To see the code example)
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 All SELECT Note: If the need to introduce external JS need to refresh to perform]

9, tostring Method
A method that uses very high rates to convert an array type object directly 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 All SELECT Note: If the need to introduce external JS need to refresh to perform]

Note: The new string is connected by the original array, but the middle has a "," delimited, if you want to remove "," You can write this:
<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 All SELECT Note: If the need to introduce external JS need to refresh to perform]

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.