I have created static array as following below
country["0"]=[USA];
sate[country[0][0]]=[["NewYork","NY"],["Ohio,"Oh"]]
for (var i = 0; i < sate[country[0][0]].length; i++) {
var key = state[country[0][0]] [i][0];
var value = state[country[0][0]] [i][i+1];
}
from above loop i am able to get the keys of state like NewYork and Ohio. Please help me how i will get the value of "NY" and "Oh"?
3 Answers 3
var value = state[country[0][0]] [i][1];
answered Oct 16, 2014 at 9:55
Christian Ezeani
3501 silver badge7 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
There are a couple or three errors in your code. Assuming country has a list of countries and state keeps the states of the country...
country = ["USA"];
state = {"USA": [["NewYork","NY"],["Ohio","Oh"]] };
for (var i = 0; i < state[country[0]].length; i++) {
var key = state[country[0]] [i][0];
var value = state[country[0]] [i][1];
}
answered Oct 16, 2014 at 10:01
dreyescat
13.9k5 gold badges52 silver badges38 bronze badges
Comments
You have mistyped here
sate[country[0][0]]=[["NewYork","NY"],["Ohio", "Oh"]]
and You can get ["NY", "Oh"] by using this:
for (var i = 0; i < sate[country[0][0]].length; i++) {
var key = state[country[0][0]] [i][0];
var value = state[country[0][0]] [i][1];
}
Comments
lang-js
["Ohio,"Oh"]is missing a"after Ohio.sateand othersstate. Wouldn't it be easier to use an object?