Mutual conversion between JavaScript Array and string

Source: Internet
Author: User
Tags array to string javascript array

The array class can be defined as follows:

VaR avalues = new array ();

If you know the length of the array in advance, you can use the parameter to pass the length

VaR avalues = new array (20 );

------------------ The following two definitions are the same -------- 1 -----------

VaR acolors = new array ();

Acolors [0] = "red ";

Acolors [1] = "green ";

Acolors [2] = "blue ";

Alert (acolors [0]); // output "red"

------------------------------------------------- 2 -----------

VaR acolors = new array ("red", "green", "blue"); // It is equivalent to array-defined arrays.

Alert (acolors [0]); // output "red" too

--------------------------

(1) convert array to string

The output of the above two array definitions is the same, and a comma separator is found in the middle.

Alert (acolors. tostring (); // output "red, green, blue ";

(2) convert string to array

We found that the array is converted to a string, with one separator ',' added between the arrays. Therefore, the string to be converted to an array must have a separator. It can be a comma or another separator.

VaR scolors = "red, green, blue ";

VaR acolors = scolors. Split (','); // the string is converted to an array.

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.