2

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.
Sam Hanley
  • 4,707
  • 7
  • 35
  • 63
Don P
  • 60,113
  • 114
  • 300
  • 432

2 Answers2

10

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.
Sam Hanley
  • 4,707
  • 7
  • 35
  • 63
1

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.

Community
  • 1
  • 1
Jack_of_All_Trades
  • 10,942
  • 18
  • 58
  • 88
  • 1
    If you are going to downvote, leave a comment please. He is trying to help. @Shmiddty, it's great that you know why this is wrong, but at some point you didn't. Instead of "tsks", please explain why. – Don P May 19 '14 at 16:56
  • Anybody, downvoting...please try it in your console.. – Jack_of_All_Trades May 19 '14 at 16:57
  • 1
    +1 It's a valid solution, and could be the best solution in some cases. – cookie monster May 19 '14 at 16:58
  • @Shmiddty: I am not saying to use eval...I will never recommend it..I am just saying it works and that answers OP's question in some sense. – Jack_of_All_Trades May 19 '14 at 16:58
  • 2
    @Jack_of_All_Trades I didn't downvote, but just because it works, doesn't mean it's good code. Typically there is no justifiable reason to use `eval` like this. Answers should provide good code, not "suggestions" – Sterling Archer May 19 '14 at 16:58
  • 3
    The downvote isn't mine but it's likely because JSON.parse() is the actual way to do this, and using eval() in this case accomplishes nothing but opening you up to exploits. – Sam Hanley May 19 '14 at 16:58
  • Jack - can you add to your answer why "eval" is a poor choice? Just dont want future people to not notice the comments – Don P May 19 '14 at 16:58
  • 1
    @sphanley: That's an overreaction to `eval`. `JSON.parse` isn't *"the actual way"*. It's one if a number of different ways. The best way to do it will depend on the overall circumstance. – cookie monster May 19 '14 at 17:00
  • 1
    1,2,3,4 may not be dangerous.. But lets look at replacing 3. a="[1,2,3,(function(){alert('XSS')})(),4]" b=eval(a). Run that in your console. – Travis J May 19 '14 at 17:00
  • @DonnyP: It is due to the optimization and injection attacks, the door that is opened by eval. I have attached a link to one SO question which discusses it. But, it is a dangerous tool and can sometime be beneficial if you know what you are doing. – Jack_of_All_Trades May 19 '14 at 17:03
  • @cookiemonster: I disagree -- I feel that if you've got a JSON string, the "actual way" to parse it into an object is the standard function which parses JSON into objects, not feeding it into a function which will blindly run any javascript code you give it. – Sam Hanley May 19 '14 at 17:03
  • @cookiemonster what can the OP use in IE8? – Loktar May 19 '14 at 17:05
  • @sphanley: If it's always going to be known to be valid JSON, then sure. If not, and you know the string is secure, then there's no trouble using `eval`. For that matter, he could do `var array = string.slice(1,-1).split(",").map(Number);` in this case. – cookie monster May 19 '14 at 17:06
  • 1
    +1 because I don't blindly follow what others have said time and time again. This is what originally got us into the mess of Eval being overused in the first place. – Loktar May 19 '14 at 17:06
  • @Loktar: `JSON.parse` will work in IE8 standards mode. – cookie monster May 19 '14 at 17:06
  • 1
    @Loktar: An interesting irony, isn't it. Good point. – cookie monster May 19 '14 at 17:07