Lets say I have a string like so:
var myStr = "[1,2,3,4,5]";
How can I convert it to something like this:
[1, 2, 3, 4, 5]
I'm trying to do this using the following command:
JSON.parse(myStr)
However, I get an error. What's the right way to do this? Moreover, can the same method be used for structured strings containing non-numbers? Like the following:
var myStr2 = "[cats, dogs, elephants]"
EDIT:
To be specific, I get this error:
SyntaxError: JSON.parse: expected ',' or ']' after array element at line 1 column 5 of the JSON data
The string part is something like this:
[16 Sep,16 Sep,16 Sep,16 Sep,16 Sep,16 Sep,16 Sep]
So I dont really understand why I get this error.
myStr2needs to look like'["cats", "dogs", "elephants"]'in order forJSON.parseto work.