Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

turn string into an array?

If I have this below as a string, how can I easily make that into an array?

"[[0.01,4.99,0.01],[5,14.95,0.05]]"

I want the same result as:

var x = [[0.01,4.99,0.01],[5,14.95,0.05]];
like image 624
badtant Avatar asked Dec 09 '22 20:12

badtant


1 Answers

var x = JSON.parse("[[0.01,4.99,0.01],[5,14.95,0.05]]");

Or the way jQuery does JSON parsing (better than eval):

var x = (new Function("return " + "[[0.01,4.99,0.01],[5,14.95,0.05]]"))();

To make this answer complete, you can use a polyfill for older browsers to support JSON.parse and JSON.stringify. I recommend json3, because Crockfords json2 is to crockfordy (insiders know what I mean).

like image 95
Prinzhorn Avatar answered Dec 28 '22 19:12

Prinzhorn