Reputation: 21
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.
Upvotes: 2
Views: 1654
Reputation: 700192
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];
Upvotes: 0
Reputation: 141859
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
Upvotes: 8