The cross-conversion of Javascript array and string.

Source: Internet
Author: User
Tags array definition javascript array

The array class can be defined as follows:

var avalues = new Array ();

If you know the length of the array beforehand, you can pass the length with the parameter

var avalues = new Array (20);

------------------the following 2 ways to define 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"); and array definition arrays are equivalent.

Alert (acolors[0]); Output "Red" too

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

(1) Array converted to string

The above 2 kinds of array definitions, the output is the same, found in the middle there is a comma separator.

Alert (acolors.tostring ()); Output "Red,green,blue";

(2) string converted to array

We found that the array was converted to a string, there were 1 more delimiters ', ' between the arrays, and then the string was converted to array arrays, which had to be delimited. It can be a comma, or it can be a separate delimiter.

var scolors = "Red,green,blue";

var acolors = Scolors.split (', '); The string is converted into an array of arrays.

The cross-conversion of Javascript array and string.

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.