Home > Article > Web Front-end > How to convert string to array in js
How to convert js arrays and strings to each other
1. Array to string
Need to use array elements Concatenate a certain character into a string. The sample code is as follows:
var a, b; a = new Array(0,1,2,3,4); b = a.join("-"); //"0-1-2-3-4"
2. Convert string to array
The implementation method is to cut the string into several strings according to a certain character, and use Return in array form, the sample code is as follows:
var s = "abc,abcd,aaa"; ss = s.split(",");// 在每个逗号(,)处进行分解 ["abc", "abcd", "aaa"] var s1 = "helloworld"; ss1 = s1.split(''); //["h", "e", "l", "l", "o", "w", "o", "r", "l", "d"]
Recommended: "javascript Advanced Tutorial"
The above is the detailed content of How to convert string to array in js. For more information, please follow other related articles on the PHP Chinese website!