1

Ive been reading semilar questions and answers trying to get a solution to this, im working on maps and getting strings that I have to convert to arrays... I got an string that's given to me as variable that I have access on it as string:

coords = '[42.46329472141537,21.46498522471254],[42.463191829327116,21.4654574564449],[42.463524249310545,21.465586246917347],[42.463642970305486,21.465092550106277]';

I need this variable to be an array, I tried JSON.parse it's throwing me an error, I tried to map(Number) that's not defined and Ive tried to split, it's deforming my array... As result I need the array to be in this format:

var newCoords = Array(Array([42.46329472141537,21.46498522471254]),Array([42.463191829327116,21.4654574564449]),Array([42.463524249310545,21.465586246917347]),Array([42.463642970305486,21.465092550106277]));

Every advice will be helpful.

5
  • 2
    Try: JSON.parse(`[${coords}]`); Commented Nov 8, 2019 at 9:54
  • Yes it did its job... Is it any likely regex expression? Commented Nov 8, 2019 at 9:56
  • No, it is just string concatenation, is the same thing as JSON.parse("[" + coords + "]"); and this just wraps the string in square brackets to have a valid JSON. Commented Nov 8, 2019 at 9:58
  • I didn't know about that, thank you very much, I was looking for a solution since 2 days... Commented Nov 8, 2019 at 10:00
  • Does this answer your question? Convert string to a multidimensional array Commented Oct 24, 2023 at 18:34

2 Answers 2

2

To make it parsable you have to wrap the arrays in a parsable object (object property, another array and so on);

To wrap your string in another object you can take advantage of template literals:

Template literals are string literals allowing embedded expressions. You can use multi-line strings and string interpolation features with them. They were called "template strings" in prior editions of the ES2015 specification.

// Wrap arrays in another array to get a parsable object

const coords = '[42.46329472141537,21.46498522471254],[42.463191829327116,21.4654574564449],[42.463524249310545,21.465586246917347],[42.463642970305486,21.465092550106277]';

console.log(JSON.parse(`[${coords}]`));

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

2 Comments

I allready got the answer on the commens, but you just changed the question it doesent have double brackeds at beginning of the string [[ it has arrays one after one on the string long...
Just edited, I thought it was trivial but if you are not used to javascript look at my edit.
1

You can use split function to make an array:

let coords = '[42.46329472141537,21.46498522471254],[42.463191829327116,21.4654574564449],[42.463524249310545,21.465586246917347],[42.463642970305486,21.465092550106277]';
let array = coords.split('],[')
              .map( c=> c.replace(/\[/g, '').replace(/]/g, '').split(','));
console.log(array);

1 Comment

Good ide, using replace after a split... Thank you for answering ^^.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.