简体   繁体   中英

How to convert String into Array in javaScript

I have a string like "[1,2,3]" and I want to convert it into array like [1,2,3] using JavaScript. Can anyone help me to do this?

Since the string you want to convert is compatible to the JSON format (JavaScript object notation), you can use JSON.parse to convert it into an array:

 const str = "[1,2,3]"; const arr = JSON.parse(str); console.log(arr);

Here you go

 var dat = "[1,2,3]"; var myData = JSON.parse(dat); console.log(myData);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM