1
[["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]]'
3
  • 1
    ["SFO,37.77493... is already an array. Commented Sep 13, 2011 at 17:15
  • var arr = eval('[["SFO",37.77493,-122.41942],["LAX",34.05223,-118.24368]]'); should give you what you are looking for. arr[0] = ["SFO"... Commented Sep 13, 2011 at 17:17
  • I've heard that eval is evil and a terrible terrible thing to use. (bytes.com/topic/javascript/answers/145037-why-eval-evil) Commented Sep 13, 2011 at 17:19

2 Answers 2

4

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
Sign up to request clarification or add additional context in comments.

1 Comment

This will convert to JSON, not an array
1
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]

Comments

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.