简体   繁体   中英

How can I convert this string into a multidimensional JavaScript array

I have a function which 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', '']]");

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