0

Is it possible to turn this:

var a = "[0,0,1,1,0,0,1,[1,0,2]]";

into an array so it will work with this?

var newArray = a; // now newArray is [0,0,1,1,0,0,1,[1,0,2]], not "[0,0,1,1,0,0,1,[1,0,2]]"
MasterPtato
  • 129
  • 2
  • 9

5 Answers5

5
var a = "[0,0,1,1,0,0,1,[1,0,2]]";
var result = JSON.parse( a );
imjared
  • 19,492
  • 4
  • 49
  • 72
  • Oh ok, that works. But i have an array with a variable name in it, a, and the parse doesn't work with strings or variables, does it? – MasterPtato Dec 05 '16 at 21:59
0
var newArray = JSON.parse(a)

That will convert from string to array.

Jake 1986
  • 582
  • 1
  • 6
  • 25
0

In addition to JSON.parse you could use eval:

var a = "[0,0,1,1,0,0,1,[1,0,2]]";
var newArray = eval(a);

As for a discussion on whether to use JSON.parse or eval, see this SO discussion: JSON.parse vs. eval()

Community
  • 1
  • 1
Scott Mitchell
  • 8,659
  • 3
  • 55
  • 71
  • There is no discussion. Never use `eval()`; – Scott Marcus Dec 05 '16 at 22:02
  • @ScottMarcus Never is such a strong word. It should be an absolute last resort and can be useful when creating a service like JSFiddle or some other kind of code playground. But it's *almost* always the wrong solution. – Mike Cluck Dec 05 '16 at 22:04
  • @MikeC I don't believe there is any use case that requires `eval()`. Most often it requires rethinking the problem. I'll stand by *never*. – Scott Marcus Dec 05 '16 at 22:24
  • @ScottMarcus Again, if you're building an app with the intent of letting the user run custom Javascript then it's the right tool for the job. You don't gain anything by writing your own JS interpreter. – Mike Cluck Dec 06 '16 at 00:19
  • @Mike C I would not use eval() even in that situation. There are always other options besides eval(). – Scott Marcus Dec 06 '16 at 12:59
  • @ScottMarcus I'm legitimately curious. How would you approach writing an abitrary JS runtime environment tool *without* `eval`? Remember: the point of your tool is to allow the user to write *any* JS and run it within some custom environment. Why would you *not* use `eval` there? If you're worried about malicious use then you can run it in an isolated container and perform static analysis on the source to identify certain classes of threats. It seems like avoiding `eval` under those circumstances is arbitrary and limiting. – Mike Cluck Dec 06 '16 at 14:56
  • @MikeC I would use `JSON.stringify` with a a "replacer" function and `JSON.parse` with a "reviver" function along with `new Function()` The `replacer` and `reviver` functions allow for static analysis. *Could* this be done with `eval()`? Sure. But, as a best-practice, I believe it's best to avoid it. Aside from your use-case, which I've already addressed and (let's be honest, is an edge-case), advocating `eval()` at all, just perpetuates its use. – Scott Marcus Dec 06 '16 at 15:26
  • @ScottMarcus `JSON.*` would only work when processing data structures and `new Function()` is largely equivalent to `eval` except for the scope. I just want to make it clear that I have been saying that it's almost always the wrong solution and is about as far from a best practice as you can get. But so many people just flat out say "never" rather than just viewing it as another very dangerous tool. I'm not suggesting it's what people should reach for but it does have it's use cases. JSFiddle, JSBin, Plunkr, CodePen, Codewars, and other education or exploratory tools are the correct use case. – Mike Cluck Dec 06 '16 at 15:39
0

Yes, it is possible, these browsers support JSON.parse(), which will parse a given String and converts it into an object, if the given string is a valid JSON representation.

0

Also you can do:

var result = eval("[0,0,1,1,0,0,1,[1,0,2]]");

You result will be:

[0, 0, 1, 1, 0, 0, 1, Array[3]]
Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108