简体   繁体   English

使用javascript / jQuery将格式化为数组的字符串转换为数组

[英]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) 进入一个javascript数组(它是一个2D数组)

Is there a way that I can do this using javascript or jQuery? 有没有办法可以使用javascript或jQuery来做到这一点?

Many Thanks! 非常感谢!

You can use JSON.parse to achieve what you require: 您可以使用JSON.parse来实现您的需求:

 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"]]');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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