How can I convert this string into a multidimensional JavaScript array
I have a function wh开发者_如何转开发ich receives a string parameter, I need to convert this into an array. For example:
var param = "['Presidente', '', ''], ['Gerente de Operaciones', 'Presidente', ''], ['Gerente de Ventas', 'Presidente', '']";
function myFunc(data){
// DoSomethingHere
}
myFunc(param);
I need to convert data into an array, in this case it would have 3 positions. I tried doing Split() but didn't get very far.
param = "[" + param + "]";
var array = JSON.parse( param );
First, make the object correct, and then use a json parser of some kind to parse the string.
You can do it with eval(). Just wrap your contents inside an extra "[ ]" to make it an array
Like so:
var data = eval("[['Presidente', '', ''], ['Gerente de Operaciones', 'Presidente', ''], ['Gerente de Ventas', 'Presidente', '']]");
精彩评论