RustBeard RustBeard - 1 year ago 85
Javascript Question

Convert string (looking like array) to multidimensional array

I have a string:

[[-3.9,-160.1,34.7],[-0.4,16.3,18.0],[236,236,231],'SMTH 123',35]

How I can convert it to a multidimensional array?

Answer Source

You can use JSON.parse() to convert a string into an object, assuming it's valid JSON to begin with. Your data has strings delimited by single quotes, which is not valid JSON. If you replace them with double quotes then it will work...

var s = "[[-3.9,-160.1,34.7],[-0.4,16.3,18.0],[236,236,231],'SMTH 123',35]";
var ar = JSON.parse(s.split("'").join("\""));

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download