es6 - How to convert string to array in JavaScript
滿天的星座
滿天的星座 2017-06-26 10:52:56
0
2
1033

For example, I have a string type like this

let str="[北京,重庆,天津]"

How can I convert it to an array type?

滿天的星座
滿天的星座

reply all(2)
过去多啦不再A梦

If it’s standard JSON, just use JSON.parse.
For non-standard ones, it depends on the specific situation. For your example:

const str="[北京,重庆,天津]";
const ans = str.slice(1, -1).split(',');
给我你的怀抱

Correct answer upstairs, it is very convenient to convert strings and arrays into each other in js.

'1,2,3'.split(',') --> ['1','2','3']

['1','2','3'].join(',') ----> '1,2,3'
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!