2

I have a string in this format:

"[0.00,0.10],[3.00,0.10],[6.00,0.10],[9.00,0.10],[12.00,662.00],[15.00,1186.00]"

I want to split it into an array so that the final array would look like this. Basically they are [x,y] pairs to use in a highcharts chart.

[[0.00,0.10], [3.00,0.10], [6.00,0.10], [9.00,0.10], [12.00,662.00], [15.00,1186.00]]

Using javascript how is this possible?

I have tried using regular expression, but cannot seem to get it to work.

Todd Miller
  • 189
  • 2
  • 9
  • Possible duplicate of https://stackoverflow.com/questions/13272406/convert-string-with-commas-to-array – Alok Aug 28 '18 at 19:22

1 Answers1

12

You could add brackets and parse as JSON with JSON.parse.

var string =  "[0.00,0.10],[3.00,0.10],[6.00,0.10],[9.00,0.10],[12.00,662.00],[15.00,1186.00]",
    array = JSON.parse('[' + string + ']');
    
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392