简体   繁体   中英

Extract values from a multidimensional array

I have a very big array with car makes and models. I've already extracted the makes into a separate array, but I am struggling to extract the models while also maintaining their association to the make.

Here is a sample of the array:

var dataa = new Array
        (
            ['Acura','','Integra','Mdx','Rl','Rsx','Slx','Tl','Tsx'],
            ['Aixam','','400','505','600'],
            ['Alfa romeo','','145','146','147','155','156'],
            ['Aston martin','','.','DBS','Db7','Db9']);

As you can see I have a multi-dimensional array with the car make (located at dataa[0][0] ), then an empty value and then the model for this make.

I am using this code to to get the car makes:

This gives me the fist value of every nested array -> dataa[i][0]:

for (var i = 0; i < dataa.length; i++) {
  document.write(dataa[i][0] + "<br>");
}

My problems start HERE.

I CAN NOT extract all models and assign them to the proper car make. I have tried for-loop's, loops with brakes, while loops and loops with conditional statements but I can't do it.

Please give me some advice here. Would jQuery or some other technology help me?

Put a loop inside your loop.

for (var i = 0; i < dataa.length; i++) {
    document.write("<h2>Starting new inner loop!</h2><br>");
    for (var j = 0; j < dataa[i].length; j++) {
        document.write(dataa[i][j] + "<br>");
    }
}

Now for every Array in the outer Array, you're doing a separate loop.

Here's a demo

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