Is there some javascript function that can take a string already formatted as an array, and casts it as an array?
var some_string = "[1,2,3,4]";
var some_array = castAsArray(some_string);
some_array.length // Returns 4.
Is there some javascript function that can take a string already formatted as an array, and casts it as an array?
var some_string = "[1,2,3,4]";
var some_array = castAsArray(some_string);
some_array.length // Returns 4.
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.