0

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"?

4
  • I imagine it's just a typo, but ["Ohio,"Oh"] is missing a " after Ohio. Commented Oct 16, 2014 at 9:54
  • 2
    That's one ugly array Commented Oct 16, 2014 at 9:56
  • I would advice to use objects instead of arrays. With an array it will get messy (assuming there will be more states included in that same array). See: w3schools.com/js/js_objects.asp Commented Oct 16, 2014 at 10:00
  • Also, sometimes you type sate and others state. Wouldn't it be easier to use an object? Commented Oct 16, 2014 at 10:13

3 Answers 3

1
var value = state[country[0][0]] [i][1];
Sign up to request clarification or add additional context in comments.

Comments

0

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];
}

Comments

0

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.