简体   繁体   中英

Javascript Accessing Multidimensional Array Keys

I have a javascript array that looks like this:

'40x27'  => array(  
    '1' => 0  
    '1.5' => 2  
    '2' = 1  
)  
'36x24' => array(   
    '1' => 1  
    '1.5' => 1  
    '2' = 2
)

etc.

I want to print out the values of the inner array like this:

i = 0;
for (i in outerArray){
    var k = 0;
    for (k in innerArray){
        alert(innerArray[k]);
    }//for
}//for

The issue I am having is that the k variable has the value of outerArray[i] instead of the key of the innerArray like so:

i=0;k="40x27";  
i=0;k="36x24";  
i=1;k="40x27";  
i=1;k="36x24";

Edit : sorry I forgot to include some code.

var outerArrays=new Array("40x27","36x24");
var innerArray=new Array("1","1.5","2");

You aren't setting innerArray to anything. Try this:

for (var i in outerArray) {
    var innerArray = outerArray[i];
    for (var k in innerArray) {
        alert(innerArray[k]);
    }
}

BTW, these are not typically called arrays in Javascript like they are in PHP. Arrays in Javascript are sequences, while objects are maps from strings to arbitrary types like in this case.

Thanks for all of the pointers, once i took a look at how I created the arrays I realized that i had a dumb syntax error:

for (i in outerArray){   
    var k = 0;  
    for(k in **innerArray[outerArray[i]]**){  
        alert(innerArray[k]);  
    } //for
} //for

thanks! you guys rock.

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