2

I have a string with a list of coordinates that I need to convert into an array. I tried to do let array = Array(coordinates) but itusing parsebut doesnt work.
The string I am trying to convert looks like this:

var coordinates="[[-118.284211,34.083625],[-118.27955,34.090733],[-118.277162,34.095776],[-118.275638,34.095363],[-118.266013,34.09563],[-118.260969,34.10031],[-118.260623,34.098253],[-118.264394,34.091552],[-118.260434,34.090493]]"

I need to remove the double quotes and it would be like this

[[[-118.284211,34.083625],[-118.27955,34.090733],[-118.277162,34.095776],[-118.275638,34.095363],[-118.266013,34.09563],[-118.260969,34.10031],[-118.260623,34.098253],[-118.264394,34.091552],[-118.260434,34.090493]]]. 

In this way it will work on kendo map. Thanks!!

gurvinder372
  • 66,980
  • 10
  • 72
  • 94
anu
  • 21
  • 3

1 Answers1

3

Simply

var output = [ JSON.parse( coordinates ) ]

Explanation :

  • Parse JSON to get internal 2-dimensional array.

  • Wrap the output of JSON.parse into another array.

gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • I already tried this but no luck . suppose my string is : var str="[[0,1],[1,0],[1,1]]" ; JSON.parse(str); it will result -Array(3) 0 : (2) [0, 1] 1 : (2) [1, 0] 2 : (2) [1, 1] length : 3 __proto__ : Array(0) . but what my need is [[0,1],[1,0],[1,1]] array with commas – anu Oct 27 '17 at 17:30