Parse a string representation of an array of arrays, into 2 separate arrays of arrays that are numbers

1348 views javascript
9

I can think of how to do this but it would get extremely complicated. So I have to turn this:

[[[0,0],[0,1],[0,2]], [[1,0],[1,1]]]

into 2 arrays:

The first would be: [[0,0], [0,1], [0,2]] - An array of arrays of numbers

The second would be: [[1,0], [1,1]] - Another array of arrays of numbers

How can I do this simply and efficiently without doing a whole bunch of string methods and checking?

answered question

Why not just parse it as JSON with JSON.parse()?

Didn't know about that

1 Answer

10

You can do something like this:

let x = "[[[0,0],[0,1],[0,2]], [[1,0],[1,1]]]"
y = JSON.parse(x)

and y would be array of arrays.

posted this

Have an answer?

JD

Please login first before posting an answer.