Converting string representation of JavaScript array to object [duplicate] Converting string representation of JavaScript array to object [duplicate] json json

Converting string representation of JavaScript array to object [duplicate]


What you're looking for is JSON.parse(). It'll take any string that represents a valid JavaScript object in JSON (JavaScript Object Notation), and convert it to an object.

var some_string = "[1,2,3,4]";var some_array = JSON.parse(some_string);some_array.length // Returns 4.


Even eval will do the trick. Using eval, is not good practice but it is just a suggestion.

a="[1,2,3,4]"b=eval(a)

Understand that using eval is always a bad idea (always means at most of the cases) and this is one excellent SO question and answers discussing this.

Eval-Don't use it.