如何将此字符串转换为多维JavaScript数组

时间:2010-08-21 03:46:22

标签: javascript arrays multidimensional-array

我有一个接收字符串参数的函数,我需要将其转换为数组。例如:

var param = "['Presidente', '', ''], ['Gerente de Operaciones', 'Presidente', ''], ['Gerente de Ventas', 'Presidente', '']";

function myFunc(data){
  // DoSomethingHere
}

myFunc(param);

我需要将数据转换为数组,在这种情况下,它将有3个位置。我尝试过做Split(),但没有走得太远。

2 个答案:

答案 0 :(得分:4)

param = "[" + param + "]";
var array = JSON.parse( param );

首先,使对象正确,然后使用某种类型的json解析器来解析字符串。

答案 1 :(得分:3)

你可以用eval()来做。只需将内容包装在一个额外的“[]”中,使其成为一个数组

像这样:

var data = eval("[['Presidente', '', ''], ['Gerente de Operaciones', 'Presidente', ''], ['Gerente de Ventas', 'Presidente', '']]");
相关问题