[英]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.