2
var tz = {"US": [123, 456, 784], "UK": [456, 461, 953]};

An I get country code from web page's form.

e.g.

var countryCode = $('#country option:selected').val(); // Now "countryCode" is UK

Now I want to get the array data of "UK". e.g. [456, 461, 953]

How can I write the code? Thanks.

1
  • 1
    tz does not contain JSON data. It is a normal object literal. It is only JSON if it is a string (and only contains valid data types of course). Commented Nov 10, 2010 at 7:22

2 Answers 2

8

Since you have the value in a variable, use the bracket notation:

tz[countryCode]

or if you know the value beforehand, this should work too.

tz.UK
Sign up to request clarification or add additional context in comments.

Comments

0

For anyone that is looking for the answer of the question in the title:

If you actually have JSON data:

var tz = '{"US": [123, 456, 784], "UK": [456, 461, 953]};';

Then you use the parseJSON method to turn it into a Javascript object, then you can access the object:

var countryData = $.parseJSON(tz);
var countryArray = countryData[countryCode];

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.