简体   繁体   中英

convert a string to array of arrays

I have a string as a prototype of an array if arrays:

'[ [7,8,9], [12,13,14] ]'

Are there any solutions to convert this to array of arrays?

I have used Array.from() with no luck.

You can use eval (usually not recommended):

 let str = '[ [7,8,9], [12,13,14] ]'; let arr = eval(str); console.log(arr);

Or JSON.parse :

 let str = '[ [7,8,9], [12,13,14] ]'; let arr = JSON.parse(str); console.log(arr);

The easiest way is to use eval but you know: eval is evil because it could be harmfull and should normally not be used.

 let str = '[ [7,8,9], [12,13,14] ]'; eval('arr='+str+';'); console.log(arr);

You can use JSON.parse() Its parse string to JS object.

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