0

In Javascript, I have the following string:

var string = "((52.33962959999999, 13.09116629999994), (52.6754542, 13.761117600000034))"

which I want to convert into an array, object, or anything to easily get to each of the numbers separately.

Right now, I'm using:

string = string.split('(').join('');
string = string.split(')').join('');
string = string.split(', ');

which gives me a one-dimensional array of the four numbers.

Is there not a nicer, easier way? One that would give me a multi-dimensional array, for example?

0

1 Answer 1

2

You could do this:

var arrayString = string.replace(/\(/g, '[').replace(/\)/g, ']');
var numberArray = JSON.parse(arrayString);

This replaces all ( with [ and ) with ]. Now you can parse it as a JavaScript object.

Output would be a two dimensional array, just as your notation.

Sign up to request clarification or add additional context in comments.

2 Comments

This is probably better than what poster asked for, although poster's answer will return these as strings, so if that's what poster needs, this'd need to be massaged a bit.
IF he have control of the formatting of the input string he could use json notation an avoid the replaces.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.