For example, I have a string type like this
let str="[北京,重庆,天津]"
How can I convert it to an array type?
If it’s standard JSON, just use JSON.parse. For non-standard ones, it depends on the specific situation. For your example:
JSON
JSON.parse
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'
If it’s standard
JSON
, just useJSON.parse
.For non-standard ones, it depends on the specific situation. For your example:
Correct answer upstairs, it is very convenient to convert strings and arrays into each other in js.