简体   繁体   中英

Turn a string formatted as an array into an array using javascript/ jQuery

I am trying to turn a string like:

"[["firstVal","secondVal"],["firstVal","secondVal"]]"

Into a javascript array (It is a 2D array)

Is there a way that I can do this using javascript or jQuery?

Many Thanks!

You can use JSON.parse to achieve what you require:

 var str = '[["firstVal","secondVal"],["firstVal","secondVal"]]'; var arr = JSON.parse(str); console.log(arr); 

Note that I had to amend the outer quotes to ' as your example string is malformed.

使用JSON.parse()方法来做到这一点:

JSON.parse('[["firstVal","secondVal"],["firstVal","secondVal"]]');

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