The example in this article describes the method of converting Array and String in Javascript. Share it with everyone for your reference, the details are as follows:
The Array class can be defined as follows:
The following two definitions are the same
Method 1:
var aColors = new Array(); aColors[0] = "red"; aColors[1] = "green"; aColors[2] = "blue"; alert(aColors[0]); // output "red"
Method 2:
var aColors = new Array("red","green","blue"); // 和Array定义数组是等同的。 alert(aColors[0]); // output "red" too
(1) Array converted to string
The output of the above two array definition methods is the same, and there is a comma separator in the middle.
We found that when Array is converted into a string, there is an extra delimiter ',' between the arrays. Then when string is converted into an Array array, there must be a delimiter. It can be a comma or other delimiter.
var sColors = "red,green,blue"; var aColors = sColors.split(','); // 字符串就转换成Array数组了。
I hope this article will be helpful to everyone in JavaScript programming.