0

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

3
  • where you you expect the right hand side 1 to come from? Commented Aug 7, 2012 at 16:25
  • What produces the initial string? What is consuming it? Have you worked with JSON before? Commented Aug 7, 2012 at 16:25
  • 1
    JSON.parse() will return the array as you want it. But why is it a string in the first place if you generated it in code? Commented Aug 7, 2012 at 16:26

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I realise that there is no 1 in the array shown. I just made that string up to give an example of what it will look like. And thank you for the example it worked perfectly!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.