Skip to content Skip to sidebar Skip to footer

Convert String Containing Arrays To Actual Arrays

I have no idea why I'm having so much trouble with this. This seems like it should be really simple. I have a JavaScript string like the following: var str = '[[1, 2, 3], [4, 5, 6

Solution 1:

The string str confirms to the JSON spec, so it can be parsed with JSON.parse.

var arr = JSON.parse(str);

Solution 2:

var str = '[[1, 2, 3], [4, 5, 6], [7, 8, 9]]';
var arr = JSON.parse("[" + str + "]");

console.log(arr[0][0]); // [1, 2, 3]console.log(arr[0][0][0]); // 1

You may use JSON.parse, more info here

https://jsfiddle.net/5yz95ktg/

Post a Comment for "Convert String Containing Arrays To Actual Arrays"