简体   繁体   中英

Converting a string to a multi-dimensional array

I have a nested for loop that creates a empty string value that represents a multidimensional array. Once the for loops have finished the result is something like this:

"[[0,0,0,0],[0,0,0,0]]"

I would like to add this to a multidimensional array within my code, how would i do this?

I have tried:

map = eval("[[0,0,0,0],[0,0,0,0]]");

but this does not produce the correct multidimensional array i am looking for.

I am looking to be able to use the array like this:

map[0][1] == 1;

Thanks

You could parse the string using JSON.parse() ( MDN docu ).

var str = "[[0,0,0,0],[0,0,0,0]]";

var map = JSON.parse( str );

However, in your example there is no entry equaling 1 , so you requirement map[0][1] == 1 wont be fulfilled that way.

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