Summary of javaScript Array Methods

Source: Internet
Author: User
Tags javascript array

1. Create an Array object (with initial values assigned) in two ways:
Var aColor = new Array ('red', 'black', 'yellow ');
Alert (aColor. toString (); // output: red, black, yellow
Var aColor = ['red', 'black', 'blue'];
Alert (aColor. toString (); // output: red, black, blue
2. length: Get the length of the array.
3. toString (): all elements in the output array.
Alert (aColor. toString (); // output: red, black, yellow
4. valueOf (): Same as 3
5. join (): concatenate the array value and connect the array with a connector. The connected string is returned.
Eg. alert (aColor. join ("->"); // output: red-> black-> yellow
// (String) 6. split (): splits the string into character arrays, where split ("") splits the string into a single character array, for example. "str" is divided into s, t, r
7. contact (): attaches the parameter to the end of the Array. The returned function value is the new Array object.
Eg.
Var aColor = new Array ('red', 'black', 'yellow ');
Var aColorCon = aColor. concat ("1", "2 ");
Alert (aColorCon. toString (); // output: red, black, yellow, 1, 2
8. slice (): returns a new array with the specified item.
9. push ()/pop (): add an item to the end of the array. pop () deletes the last array item and returns the value.
Eg.
Var aColor = new Array ('red', 'black', 'yellow ');
AColor. push ("blue ");
AColor. push ("white ");
Alert (aColor. toString ());
AColor. pop ();
Alert (aColor. toString ());
10. shift (): Delete the first entry in the array.
11. unshift (): add the first item to the array, and move the other items backward.
Var aColor = new Array ('red', 'black', 'yellow ');
AColor. shift ();
Alert (aColor. toString ());
AColor. unshift ("red ");
Alert (aColor. toString ());
12. reverse (): reverse the order of array items.
13. sort (): sort array items in ascending order.
Eg.
Var aColor = new Array ('red', 'black', 'yellow ');
AColor. sort ();
Alert (aColor );
Alert (aColor. reverse ());
14. splice ().

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.