简体   繁体   中英

How to get value of key from the array in java script

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

var value = state[country[0][0]] [i][1];

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM