[英]How can I convert this string into a multidimensional JavaScript array
我有一个接收字符串参数的函数,我需要将其转换为数组。 例如:
var param = "['Presidente', '', ''], ['Gerente de Operaciones', 'Presidente', ''], ['Gerente de Ventas', 'Presidente', '']";
function myFunc(data){
// DoSomethingHere
}
myFunc(param);
我需要将数据转换为数组,在这种情况下,它将具有3个位置。 我试着做Split(),但是走得并不远。
param = "[" + param + "]";
var array = JSON.parse( param );
首先,使对象正确,然后使用某种json解析器解析字符串。
您可以使用eval()来完成。 只需将您的内容包装在一个额外的“ []”中以使其成为数组
像这样:
var data = eval("[['Presidente', '', ''], ['Gerente de Operaciones', 'Presidente', ''], ['Gerente de Ventas', 'Presidente', '']]");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.