[["SFO",37.77493,-122.41942],["LAX",34.05223,-118.24368]]
That's my string and I want to convert it to:
arr[0] = ["SFO"....
arr[1] = ["LAX"...
EDIT
Let me clarify:
var str = '[["SFO",37.77493,-122.41942],["LAX",34.05223,-118.24368]]'
asked Sep 13, 2011 at 17:12
Shamoon
43.9k106 gold badges337 silver badges637 bronze badges
2 Answers 2
You can use JSON.parse:
JSON.parse('[["SFO",37.77493,-122.41942],["LAX",34.05223,-118.24368]]')
IE7 and below needs:
<!--[if lt IE 8.]>
<script src="http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script>
<![endif]-->
JSON.parse converts a string into a javascript object. Array's are objects. The string above will parse into an Array:
var array = JSON.parse('[["SFO",37.77493,-122.41942],["LAX",34.05223,-118.24368]]')
alert ( typeof array ); // object
alert ( array instanceof Array); // true
answered Sep 13, 2011 at 17:19
Joe
82.9k18 gold badges130 silver badges147 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Shamoon
This will convert to JSON, not an array
var arr = [["SFO",37.77493,-122.41942],["LAX",34.05223,-118.24368]];
console.log(arr[0]); // ["SFO", 37.77493, -122.41942]
console.log(arr[1]); // ["LAX", 34.05223, -118.24368]
answered Sep 13, 2011 at 17:16
Sahil Muthoo
12.7k2 gold badges32 silver badges39 bronze badges
Comments
lang-js
["SFO,37.77493...is already an array.var arr = eval('[["SFO",37.77493,-122.41942],["LAX",34.05223,-118.24368]]');should give you what you are looking for.arr[0] = ["SFO"...evalis evil and a terrible terrible thing to use. (bytes.com/topic/javascript/answers/145037-why-eval-evil)